-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from mittwald/feature/error-code-mapping
feat: add library code for mapping HTTP response errors onto specific error types
- Loading branch information
Showing
3 changed files
with
48 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package httperr | ||
|
||
import "net/http" | ||
|
||
// ErrFromResponse maps an HTTP response (with an error status code) to a (more | ||
// or less) specific error type. | ||
func ErrFromResponse(res *http.Response) error { | ||
switch res.StatusCode { | ||
case http.StatusNotFound: | ||
return &ErrNotFound{Response: res} | ||
case http.StatusForbidden: | ||
return &ErrPermissionDenied{Response: res} | ||
default: | ||
return &ErrUnexpectedResponse{Response: res} | ||
} | ||
} |
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,16 @@ | ||
package httperr | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// ErrNotFound represents the error of trying to access a resource that does | ||
// not exist. | ||
type ErrNotFound struct { | ||
Response *http.Response | ||
} | ||
|
||
func (e *ErrNotFound) Error() string { | ||
return fmt.Sprintf("resource not found: %s", e.Response.Request.URL) | ||
} |
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,16 @@ | ||
package httperr | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// ErrPermissionDenied represents the error of trying to access a resource that | ||
// the currently authenticated user is not authorized to access. | ||
type ErrPermissionDenied struct { | ||
Response *http.Response | ||
} | ||
|
||
func (e *ErrPermissionDenied) Error() string { | ||
return fmt.Sprintf("permission to resource denied: %s", e.Response.Request.URL) | ||
} |