Skip to content

Commit

Permalink
update error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
saturninoabril committed Oct 10, 2018
1 parent 9f97760 commit 597bec0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Req
func (p *Plugin) getGo(w http.ResponseWriter, r *http.Request) {
userID := r.Header.Get("Mattermost-User-ID")
if userID == "" {
http.Error(w, "Not authorized", http.StatusUnauthorized)
http.Error(w, "Not authorized to translate post", http.StatusUnauthorized)
return
}

Expand All @@ -67,7 +67,7 @@ func (p *Plugin) getGo(w http.ResponseWriter, r *http.Request) {

target := r.URL.Query().Get("target")
if len(target) != 2 {
http.Error(w, "Invalid parameter: source", http.StatusBadRequest)
http.Error(w, "Invalid parameter: target", http.StatusBadRequest)
return
}

Expand Down Expand Up @@ -95,7 +95,7 @@ func (p *Plugin) getGo(w http.ResponseWriter, r *http.Request) {

output, awsErr := svc.Text(&input)
if awsErr != nil {
http.Error(w, awsErr.Error(), http.StatusInternalServerError)
http.Error(w, awsErr.Error(), http.StatusBadRequest)
return
}

Expand All @@ -116,13 +116,13 @@ func (p *Plugin) getGo(w http.ResponseWriter, r *http.Request) {
func (p *Plugin) getInfo(w http.ResponseWriter, r *http.Request) {
userID := r.Header.Get("Mattermost-User-ID")
if userID == "" {
http.Error(w, "Not authorized", http.StatusUnauthorized)
// silently return as user is probably not logged in
return
}

info, err := p.getUserInfo(userID)
if err != nil {
http.Error(w, "Failed to get info", http.StatusInternalServerError)
// silently return as user may not have activated the autotranslation
return
}

Expand All @@ -133,7 +133,7 @@ func (p *Plugin) getInfo(w http.ResponseWriter, r *http.Request) {
func (p *Plugin) setInfo(w http.ResponseWriter, r *http.Request) {
userID := r.Header.Get("Mattermost-User-ID")
if userID == "" {
http.Error(w, "Not authorized", http.StatusUnauthorized)
http.Error(w, "Not authorized to set info", http.StatusUnauthorized)
return
}

Expand All @@ -156,7 +156,7 @@ func (p *Plugin) setInfo(w http.ResponseWriter, r *http.Request) {

err := p.setUserInfo(info)
if err != nil {
http.Error(w, "Failed to set info", http.StatusInternalServerError)
http.Error(w, "Failed to set info", http.StatusBadRequest)
return
}

Expand Down
6 changes: 3 additions & 3 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (p *Plugin) getUserInfo(userID string) (*UserInfo, *APIErrorResponse) {
if infoBytes, err := p.API.KVGet(userID); err != nil || infoBytes == nil {
return nil, &APIErrorResponse{ID: API_ERROR_NO_RECORD_FOUND, Message: "No record found.", StatusCode: http.StatusBadRequest}
} else if err := json.Unmarshal(infoBytes, &userInfo); err != nil {
return nil, &APIErrorResponse{ID: "unable_to_unmarshal", Message: "Unable to unmarshal json.", StatusCode: http.StatusInternalServerError}
return nil, &APIErrorResponse{ID: "unable_to_unmarshal", Message: "Unable to unmarshal json.", StatusCode: http.StatusBadRequest}
}

return &userInfo, nil
Expand All @@ -136,11 +136,11 @@ func (p *Plugin) setUserInfo(userInfo *UserInfo) *APIErrorResponse {

jsonUserInfo, err := json.Marshal(userInfo)
if err != nil {
return &APIErrorResponse{ID: "unable_to_unmarshal", Message: "Unable to marshal json.", StatusCode: http.StatusInternalServerError}
return &APIErrorResponse{ID: "unable_to_unmarshal", Message: "Unable to marshal json.", StatusCode: http.StatusBadRequest}
}

if err := p.API.KVSet(userInfo.UserID, jsonUserInfo); err != nil {
return &APIErrorResponse{ID: "unable_to_save", Message: "Unable to save user info.", StatusCode: http.StatusInternalServerError}
return &APIErrorResponse{ID: "unable_to_save", Message: "Unable to save user info.", StatusCode: http.StatusBadRequest}
}

p.emitUserInfoChange(userInfo)
Expand Down

0 comments on commit 597bec0

Please sign in to comment.