-
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.
Merge branch 'develop' of https://github.com/helxplatform/gitea-assist …
…into list-commits
- Loading branch information
Showing
4 changed files
with
466 additions
and
440 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package errorapi | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
type APIError struct { | ||
error | ||
status int | ||
} | ||
|
||
func (e APIError) Error() string { | ||
msg := e.error.Error() | ||
// The join method while wrapping concatenates error messages with newlines. Replace newlines with spaces | ||
return strings.Replace(msg, "\n", " ", -1) | ||
} | ||
|
||
func (e APIError) Compare(err *APIError) bool { | ||
return errors.Is(e.error, err.error) | ||
} | ||
|
||
var ( | ||
// A list of general error of type APIError | ||
ErrBadRequest = &APIError{error: errors.New("bad request"), status: http.StatusBadRequest} | ||
ErrNotFound = &APIError{error: errors.New("not found"), status: http.StatusNotFound} | ||
ErrInternalServerError = &APIError{error: errors.New("internal server error"), status: http.StatusInternalServerError} | ||
ErrRequestReadError = &APIError{error: errors.New("error reading request body"), status: http.StatusBadRequest} | ||
ErrResponseReadError = &APIError{error: errors.New("error reading response body"), status: http.StatusBadRequest} | ||
ErrRequestParseError = &APIError{error: errors.New("request parse error"), status: http.StatusBadRequest} | ||
ErrMethodNotAllowed = &APIError{error: errors.New("method not allowed"), status: http.StatusMethodNotAllowed} | ||
ErrGiteaConnectError = &APIError{error: errors.New("error connecting to gitea"), status: http.StatusBadRequest} | ||
ErrUnauthorized = &APIError{error: errors.New("unauthorized attempt to login"), status: http.StatusUnauthorized} | ||
// Making a slice for all predefined errors for ease of comparison in HandleError below | ||
allErrors = []*APIError{ErrBadRequest, ErrNotFound, ErrInternalServerError, ErrRequestReadError, ErrResponseReadError, ErrMethodNotAllowed, ErrRequestParseError, ErrMethodNotAllowed, ErrGiteaConnectError, ErrUnauthorized} | ||
) | ||
|
||
// This function provides capability to "modify" the message of an existing error | ||
// but still keep the original type when comparing | ||
// For example: | ||
// repoError := WrapError(ErrNotFound, "Repo") :: Creates a new error specific to repo | ||
// But now, repoError.Compare(ErrNotFound) will return true because of the Join method of errors package | ||
func WrapError(e *APIError, msg string) *APIError { | ||
err := errors.Join(fmt.Errorf("%v", msg), e.error) | ||
return &APIError{error: err, status: e.status} | ||
} | ||
|
||
func HandleError(w http.ResponseWriter, err *APIError) { | ||
for _, sperr := range allErrors { | ||
if err.Compare(sperr) { | ||
http.Error(w, err.Error(), err.status) | ||
log.Printf("ERROR: %s STATUS: %d\n", err.Error(), err.status) | ||
return | ||
} | ||
} | ||
http.Error(w, ErrInternalServerError.Error(), ErrInternalServerError.status) | ||
log.Printf("Unknown error occured %v", err) | ||
} |
Oops, something went wrong.