-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
adb79dc
commit 18a409e
Showing
5 changed files
with
120 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
) | ||
|
||
// Structure of an error response from the API | ||
type errorResponse struct { | ||
Message string `json:"message"` | ||
Error string `json:"error"` | ||
StatusCode int `json:"statusCode"` | ||
} | ||
|
||
// Returns an error following the HTTP response code: | ||
// | ||
// 4xx: Gives the error messages from the response body | ||
// 5xx: Gives a generic error message | ||
// | ||
// If the error code is not one of these, no error is returned | ||
func Error(resp *http.Response) error { | ||
switch { | ||
// handle 4xx errors | ||
case resp.StatusCode >= 400 && resp.StatusCode < 500: | ||
var respErr errorResponse | ||
err := json.NewDecoder(resp.Body).Decode(&respErr) | ||
if err != nil { | ||
return err | ||
} | ||
return errors.New(respErr.Message) | ||
|
||
// handle 4xx errors | ||
case resp.StatusCode >= 500 && resp.StatusCode < 600: | ||
// handles 5xx errors | ||
return errors.New("Server error") | ||
|
||
// handle other status codes | ||
default: | ||
return nil | ||
} | ||
} |