-
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
c317a1a
commit bd2f813
Showing
12 changed files
with
157 additions
and
73 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,30 @@ | ||
package httperror | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
type HTTPError struct { | ||
Message string `json:"message"` | ||
StatusCode int `json:"status_code"` | ||
} | ||
|
||
func (e *HTTPError) WriteError(w http.ResponseWriter) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(e.StatusCode) | ||
json.NewEncoder(w).Encode(e) | ||
} | ||
|
||
var ( | ||
ErrInvalidID = &HTTPError{Message: "Invalid ID", StatusCode: http.StatusBadRequest} | ||
ErrNotFound = &HTTPError{Message: "Resource not found", StatusCode: http.StatusNotFound} | ||
ErrInternalServer = &HTTPError{Message: "Internal server error", StatusCode: http.StatusInternalServerError} | ||
) | ||
|
||
func NewHTTPError(message string, statusCode int) *HTTPError { | ||
return &HTTPError{ | ||
Message: message, | ||
StatusCode: statusCode, | ||
} | ||
} |
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
Oops, something went wrong.