-
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.
* Add 401, 403 error types * Migrate HTTP error serving to apierrors package For better separation of concerns between the router and the API error handling/HTTP error generation. * Add TODO * Use abstraction for auth middleware error handling Prevents code duplication and improves separation of concerns. Also paves the way for easier future changes to error messages when they become more structured. * Update users, stories controller error handling Ensures consistency and improves correctness. * Fix error message typo
- Loading branch information
1 parent
d918637
commit c45af08
Showing
7 changed files
with
78 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package apierrors | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
type ClientUnauthorizedError struct { | ||
Message string | ||
} | ||
|
||
func (e ClientUnauthorizedError) Error() string { | ||
return e.Message | ||
} | ||
|
||
func (e ClientUnauthorizedError) HTTPStatusCode() int { | ||
return http.StatusUnauthorized | ||
} |
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,17 @@ | ||
package apierrors | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
type ClientForbiddenError struct { | ||
Message string | ||
} | ||
|
||
func (e ClientForbiddenError) Error() string { | ||
return e.Message | ||
} | ||
|
||
func (e ClientForbiddenError) HTTPStatusCode() int { | ||
return http.StatusForbidden | ||
} |
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 |
---|---|---|
@@ -1,8 +1,25 @@ | ||
package apierrors | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
) | ||
|
||
// ClientError is an interface for errors that should be returned to the client. | ||
// They generally start with a 4xx HTTP status code. | ||
type ClientError interface { | ||
error | ||
HTTPStatusCode() int | ||
} | ||
|
||
func ServeHTTP(w http.ResponseWriter, r *http.Request, err error) { | ||
var clientError ClientError | ||
if errors.As(err, &clientError) { | ||
// Client error (status 4xx), write error message and status code | ||
http.Error(w, clientError.Error(), clientError.HTTPStatusCode()) | ||
return | ||
} | ||
|
||
// 500 Internal Server Error as a catch-all | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} |
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