File tree Expand file tree Collapse file tree 3 files changed +48
-0
lines changed
Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ package httperr
2+
3+ import "net/http"
4+
5+ // ErrFromResponse maps an HTTP response (with an error status code) to a (more
6+ // or less) specific error type.
7+ func ErrFromResponse (res * http.Response ) error {
8+ switch res .StatusCode {
9+ case http .StatusNotFound :
10+ return & ErrNotFound {Response : res }
11+ case http .StatusForbidden :
12+ return & ErrPermissionDenied {Response : res }
13+ default :
14+ return & ErrUnexpectedResponse {Response : res }
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ package httperr
2+
3+ import (
4+ "fmt"
5+ "net/http"
6+ )
7+
8+ // ErrNotFound represents the error of trying to access a resource that does
9+ // not exist.
10+ type ErrNotFound struct {
11+ Response * http.Response
12+ }
13+
14+ func (e * ErrNotFound ) Error () string {
15+ return fmt .Sprintf ("resource not found: %s" , e .Response .Request .URL )
16+ }
Original file line number Diff line number Diff line change 1+ package httperr
2+
3+ import (
4+ "fmt"
5+ "net/http"
6+ )
7+
8+ // ErrPermissionDenied represents the error of trying to access a resource that
9+ // the currently authenticated user is not authorized to access.
10+ type ErrPermissionDenied struct {
11+ Response * http.Response
12+ }
13+
14+ func (e * ErrPermissionDenied ) Error () string {
15+ return fmt .Sprintf ("permission to resource denied: %s" , e .Response .Request .URL )
16+ }
You can’t perform that action at this time.
0 commit comments