mirror of
https://github.com/portainer/portainer.git
synced 2025-08-02 12:25:22 +02:00
feat(libhttp): move into the Portainer repository EE-5475 (#10231)
This commit is contained in:
parent
090fa4aeb3
commit
8cc5e0796c
249 changed files with 1059 additions and 639 deletions
46
pkg/libhttp/error/error.go
Normal file
46
pkg/libhttp/error/error.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
// Package error provides error/logging functions that can be used in conjuction with http.Handler.
|
||||
package error
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type (
|
||||
// LoggerHandler defines a HTTP handler that includes a HandlerError return pointer
|
||||
LoggerHandler func(http.ResponseWriter, *http.Request) *HandlerError
|
||||
|
||||
errorResponse struct {
|
||||
Message string `json:"message,omitempty"`
|
||||
Details string `json:"details,omitempty"`
|
||||
}
|
||||
)
|
||||
|
||||
func (handler LoggerHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||
err := handler(rw, r)
|
||||
if err != nil {
|
||||
writeErrorResponse(rw, err)
|
||||
}
|
||||
}
|
||||
|
||||
func writeErrorResponse(rw http.ResponseWriter, err *HandlerError) {
|
||||
if err.Err == nil {
|
||||
err.Err = errors.New(err.Message)
|
||||
}
|
||||
|
||||
log.Debug().CallerSkipFrame(2).Err(err.Err).Int("status_code", err.StatusCode).Str("msg", err.Message).Msg("HTTP error")
|
||||
|
||||
rw.Header().Set("Content-Type", "application/json")
|
||||
rw.WriteHeader(err.StatusCode)
|
||||
|
||||
json.NewEncoder(rw).Encode(&errorResponse{Message: err.Message, Details: err.Err.Error()})
|
||||
}
|
||||
|
||||
// WriteError is a convenience function that creates a new HandlerError before calling writeErrorResponse.
|
||||
// For use outside of the standard http handlers.
|
||||
func WriteError(rw http.ResponseWriter, code int, message string, err error) {
|
||||
writeErrorResponse(rw, &HandlerError{code, message, err})
|
||||
}
|
42
pkg/libhttp/error/status.go
Normal file
42
pkg/libhttp/error/status.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
package error
|
||||
|
||||
import "net/http"
|
||||
|
||||
// HandlerError represents an error raised inside a HTTP handler
|
||||
type HandlerError struct {
|
||||
StatusCode int
|
||||
Message string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (h *HandlerError) Error() string {
|
||||
return h.Message
|
||||
}
|
||||
|
||||
func NewError(statusCode int, message string, err error) *HandlerError {
|
||||
return &HandlerError{
|
||||
StatusCode: statusCode,
|
||||
Message: message,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
|
||||
func BadRequest(message string, err error) *HandlerError {
|
||||
return NewError(http.StatusBadRequest, message, err)
|
||||
}
|
||||
|
||||
func NotFound(message string, err error) *HandlerError {
|
||||
return NewError(http.StatusNotFound, message, err)
|
||||
}
|
||||
|
||||
func InternalServerError(message string, err error) *HandlerError {
|
||||
return NewError(http.StatusInternalServerError, message, err)
|
||||
}
|
||||
|
||||
func Unauthorized(message string, err error) *HandlerError {
|
||||
return NewError(http.StatusUnauthorized, message, err)
|
||||
}
|
||||
|
||||
func Forbidden(message string, err error) *HandlerError {
|
||||
return NewError(http.StatusForbidden, message, err)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue