Files
ktm-booking-bot/backend/internal/common/errresponse.go
Samuel Pua 7cf10b07d4
Some checks failed
ktm-booking-bot/ktm-booking-bot/pipeline/head Something is wrong with the build of this commit
Feat(ktm-booking): Initial commit
2022-09-27 02:50:07 +08:00

57 lines
1.3 KiB
Go

package common
import (
"net/http"
"github.com/go-chi/render"
)
type ErrResponse struct {
Err error `json:"-"` // low-level runtime error
HTTPStatusCode int `json:"-"` // http response status code
StatusText string `json:"status"` // user-level status message
AppCode int64 `json:"code,omitempty"` // application-specific error code
ErrorText string `json:"error,omitempty"` // application-level error message, for debugging
}
func (e *ErrResponse) Render(w http.ResponseWriter, r *http.Request) error {
render.Status(r, e.HTTPStatusCode)
return nil
}
func ErrInvalidRequest(err error) render.Renderer {
return &ErrResponse{
Err: err,
HTTPStatusCode: 400,
StatusText: "Invalid request.",
ErrorText: err.Error(),
}
}
func ErrValidationError(err error) render.Renderer {
return &ErrResponse{
Err: err,
HTTPStatusCode: 422,
StatusText: "Validation error.",
ErrorText: err.Error(),
}
}
func ErrInternalError(err error) render.Renderer {
return &ErrResponse{
Err: err,
HTTPStatusCode: 500,
StatusText: "Invalid request.",
ErrorText: err.Error(),
}
}
func ErrNotFound(err error) render.Renderer {
return &ErrResponse{
HTTPStatusCode: 404,
StatusText: "Resource not found.",
ErrorText: err.Error(),
}
}