Skip to content

Commit f69b381

Browse files
author
Matthew Fisher
committed
fix(errors): display the error on a 404
Some 404 error messages like when an app has not been found returns a specific message for the user. Returning a generic "Not Found" message is not helpful.
1 parent de2ae41 commit f69b381

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

errors.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ const (
3535
)
3636

3737
var (
38-
// ErrNotFound is returned when the server returns a 404.
39-
ErrNotFound = errors.New("Not Found")
4038
// ErrServerError is returned when the server returns a 500.
4139
ErrServerError = errors.New("Internal Server Error")
4240
// ErrMethodNotAllowed is thrown when using a unsupposrted method.
@@ -93,10 +91,19 @@ type ErrUnprocessable struct {
9391
errorMsg string
9492
}
9593

94+
// ErrNotFound is returned when the controller throws a 404.
95+
type ErrNotFound struct {
96+
errorMsg string
97+
}
98+
9699
func (e ErrUnprocessable) Error() string {
97100
return fmt.Sprintf("Unable to process your request: %s", e.errorMsg)
98101
}
99102

103+
func (e ErrNotFound) Error() string {
104+
return e.errorMsg
105+
}
106+
100107
// checkForErrors tries to match up an API error with an predefined error in the SDK.
101108
func checkForErrors(res *http.Response) error {
102109
if res.StatusCode >= 200 && res.StatusCode < 400 {
@@ -197,7 +204,10 @@ func checkForErrors(res *http.Response) error {
197204
case 403:
198205
return ErrForbidden
199206
case 404:
200-
return ErrNotFound
207+
if string(out) != "" {
208+
return ErrNotFound{string(out)}
209+
}
210+
return ErrNotFound{"Not Found"}
201211
case 405:
202212
return ErrMethodNotAllowed
203213
case 409:

errors_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,14 @@ func TestErrors(t *testing.T) {
241241
StatusCode: 404,
242242
Body: readCloser(""),
243243
},
244-
expected: ErrNotFound,
244+
expected: ErrNotFound{"Not Found"},
245+
},
246+
{
247+
res: &http.Response{
248+
StatusCode: 404,
249+
Body: readCloser("App not found"),
250+
},
251+
expected: ErrNotFound{"App not found"},
245252
},
246253
{
247254
res: &http.Response{

0 commit comments

Comments
 (0)