2022-05-02 08:19:37 -07:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2022-05-14 07:52:08 -07:00
|
|
|
"codeberg.org/u1f320/pronouns.cc/backend/log"
|
2022-05-02 08:19:37 -07:00
|
|
|
"github.com/go-chi/render"
|
|
|
|
)
|
|
|
|
|
|
|
|
// WrapHandler wraps a modified http.HandlerFunc into a stdlib-compatible one.
|
|
|
|
// The inner HandlerFunc additionally returns an error.
|
|
|
|
func WrapHandler(hn func(w http.ResponseWriter, r *http.Request) error) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := hn(w, r)
|
|
|
|
if err != nil {
|
|
|
|
// if the function returned an API error, just render that verbatim
|
|
|
|
// we can assume that it also logged the error (if that was needed)
|
|
|
|
if apiErr, ok := err.(APIError); ok {
|
|
|
|
apiErr.prepare()
|
|
|
|
|
|
|
|
render.Status(r, apiErr.Status)
|
|
|
|
render.JSON(w, r, apiErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, we log the error and return an internal server error message
|
|
|
|
log.Errorf("error in http handler: %v", err)
|
|
|
|
|
|
|
|
apiErr := APIError{Code: ErrInternalServerError}
|
|
|
|
apiErr.prepare()
|
|
|
|
|
|
|
|
render.Status(r, apiErr.Status)
|
|
|
|
render.JSON(w, r, apiErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// APIError is an object returned by the API when an error occurs.
|
|
|
|
// It implements the error interface and can be returned by handlers.
|
|
|
|
type APIError struct {
|
|
|
|
Code int `json:"code"`
|
|
|
|
Message string `json:"message,omitempty"`
|
2022-05-17 13:35:26 -07:00
|
|
|
Details string `json:"details,omitempty"`
|
2022-05-02 08:19:37 -07:00
|
|
|
|
2022-05-25 15:41:06 -07:00
|
|
|
RatelimitReset *int `json:"ratelimit_reset,omitempty"`
|
|
|
|
|
2022-05-17 13:35:26 -07:00
|
|
|
// Status is set as the HTTP status code.
|
2022-05-02 08:19:37 -07:00
|
|
|
Status int `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e APIError) Error() string {
|
|
|
|
return fmt.Sprintf("%s (code: %d)", e.Message, e.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *APIError) prepare() {
|
|
|
|
if e.Status == 0 {
|
|
|
|
e.Status = errCodeStatuses[e.Code]
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Message == "" {
|
|
|
|
e.Message = errCodeMessages[e.Code]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error code constants
|
|
|
|
const (
|
2022-05-04 07:27:16 -07:00
|
|
|
ErrBadRequest = 400
|
|
|
|
ErrForbidden = 403
|
2022-05-17 13:35:26 -07:00
|
|
|
ErrNotFound = 404
|
|
|
|
ErrMethodNotAllowed = 405
|
2022-05-25 15:41:06 -07:00
|
|
|
ErrTooManyRequests = 429
|
2022-05-02 08:19:37 -07:00
|
|
|
ErrInternalServerError = 500 // catch-all code for unknown errors
|
2022-05-04 07:27:16 -07:00
|
|
|
|
|
|
|
// Login/authorize error codes
|
|
|
|
ErrInvalidState = 1001
|
|
|
|
ErrInvalidOAuthCode = 1002
|
2022-05-14 12:55:44 -07:00
|
|
|
ErrInvalidToken = 1003 // a token was supplied, but it is invalid
|
2022-05-04 07:27:16 -07:00
|
|
|
|
|
|
|
// User-related error codes
|
|
|
|
ErrUserNotFound = 2001
|
2022-05-02 08:19:37 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var errCodeMessages = map[int]string{
|
2022-05-04 07:27:16 -07:00
|
|
|
ErrBadRequest: "Bad request",
|
|
|
|
ErrForbidden: "Forbidden",
|
2022-05-02 08:19:37 -07:00
|
|
|
ErrInternalServerError: "Internal server error",
|
2022-05-17 13:35:26 -07:00
|
|
|
ErrNotFound: "Not found",
|
2022-05-25 15:41:06 -07:00
|
|
|
ErrTooManyRequests: "Rate limit reached",
|
2022-05-17 13:35:26 -07:00
|
|
|
ErrMethodNotAllowed: "Method not allowed",
|
2022-05-04 07:27:16 -07:00
|
|
|
|
|
|
|
ErrInvalidState: "Invalid OAuth state",
|
|
|
|
ErrInvalidOAuthCode: "Invalid OAuth code",
|
2022-05-14 12:55:44 -07:00
|
|
|
ErrInvalidToken: "Supplied token was invalid",
|
2022-05-04 07:27:16 -07:00
|
|
|
|
|
|
|
ErrUserNotFound: "User not found",
|
2022-05-02 08:19:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var errCodeStatuses = map[int]int{
|
2022-05-04 07:27:16 -07:00
|
|
|
ErrBadRequest: http.StatusBadRequest,
|
|
|
|
ErrForbidden: http.StatusForbidden,
|
2022-05-02 08:19:37 -07:00
|
|
|
ErrInternalServerError: http.StatusInternalServerError,
|
2022-05-17 13:35:26 -07:00
|
|
|
ErrNotFound: http.StatusNotFound,
|
2022-05-25 15:41:06 -07:00
|
|
|
ErrTooManyRequests: http.StatusTooManyRequests,
|
2022-05-17 13:35:26 -07:00
|
|
|
ErrMethodNotAllowed: http.StatusMethodNotAllowed,
|
2022-05-04 07:27:16 -07:00
|
|
|
|
|
|
|
ErrInvalidState: http.StatusBadRequest,
|
|
|
|
ErrInvalidOAuthCode: http.StatusForbidden,
|
2022-05-14 12:55:44 -07:00
|
|
|
ErrInvalidToken: http.StatusUnauthorized,
|
2022-05-04 07:27:16 -07:00
|
|
|
|
|
|
|
ErrUserNotFound: http.StatusNotFound,
|
2022-05-02 08:19:37 -07:00
|
|
|
}
|