github.com/lastbackend/toolkit@v0.0.0-20241020043710-cafa37b95aad/pkg/server/http/errors/errors.go (about)

     1  package errors
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  const (
    11  	StatusNotFound            = "Not Found"
    12  	StatusBadParameter        = "Bad Parameter"
    13  	StatusInUse               = "In use"
    14  	StatusBadRequest          = "Bad Request"
    15  	StatusUnknown             = "Unknown"
    16  	StatusIncorrectXml        = "Incorrect Xml"
    17  	StatusIncorrectJson       = "Incorrect Json"
    18  	StatusNotUnique           = "Not Unique"
    19  	StatusInternalServerError = "Internal Server Error"
    20  	StatusForbidden           = "Forbidden"
    21  	StatusNotAllowed          = "Not Allowed"
    22  )
    23  
    24  const (
    25  	ErrorInviteNotFound = iota
    26  	ErrorUsernameInUse
    27  	ErrorEmailInUse
    28  	ErrorAccountNotFound
    29  	ErrorNotUnique
    30  	ErrorAlreadyAttached
    31  	ErrorNoAttached
    32  	ErrorUnknown
    33  )
    34  
    35  type ErrorCode int
    36  
    37  var errorMessages = map[ErrorCode]string{
    38  	ErrorInviteNotFound:  "Invite not found",
    39  	ErrorUsernameInUse:   "Username in use",
    40  	ErrorEmailInUse:      "Email in use",
    41  	ErrorAccountNotFound: "OwnerId not found",
    42  	ErrorNotUnique:       "Not unique",
    43  	ErrorAlreadyAttached: "Already attached",
    44  	ErrorNoAttached:      "Mo attached",
    45  	ErrorUnknown:         "Unknown error not found",
    46  }
    47  
    48  func GetError(code ErrorCode) error {
    49  	if message, ok := errorMessages[code]; ok {
    50  		return errors.New(message)
    51  	} else {
    52  		return errors.New(errorMessages[ErrorUnknown])
    53  	}
    54  }
    55  
    56  func GetErrorMessage(code ErrorCode) string {
    57  	if message, ok := errorMessages[code]; ok {
    58  		return message
    59  	} else {
    60  		return errorMessages[ErrorUnknown]
    61  	}
    62  }
    63  
    64  type Err struct {
    65  	Code   string
    66  	Attr   string
    67  	origin error
    68  	http   *Http
    69  }
    70  
    71  func BadParameter(attr string, e ...error) *Err {
    72  	return &Err{
    73  		Code:   StatusBadParameter,
    74  		Attr:   attr,
    75  		origin: getError(attr+": bad parameter", e...),
    76  		http:   HTTP.getBadParameter(attr),
    77  	}
    78  }
    79  
    80  func IncorrectJSON(e ...error) *Err {
    81  	return &Err{
    82  		Code:   StatusIncorrectJson,
    83  		origin: getError("incorrect json", e...),
    84  		http:   HTTP.getIncorrectJSON(),
    85  	}
    86  }
    87  
    88  func IncorrectXML(e ...error) *Err {
    89  	return &Err{
    90  		Code:   StatusIncorrectXml,
    91  		origin: getError("incorrect xml", e...),
    92  		http:   HTTP.getIncorrectJSON(),
    93  	}
    94  }
    95  
    96  func Forbidden(e ...error) *Err {
    97  	return &Err{
    98  		Code:   StatusForbidden,
    99  		origin: getError("forbidden", e...),
   100  		http:   HTTP.getForbidden(),
   101  	}
   102  }
   103  
   104  func NotAllowed(e ...error) *Err {
   105  	return &Err{
   106  		Code:   StatusNotAllowed,
   107  		origin: getError("not allowed", e...),
   108  		http:   HTTP.getNotAllowed(),
   109  	}
   110  }
   111  
   112  func Unknown(e ...error) *Err {
   113  	return &Err{
   114  		Code:   StatusUnknown,
   115  		origin: getError("unknown error", e...),
   116  		http:   HTTP.getUnknown(),
   117  	}
   118  }
   119  
   120  func (e *Err) Err() error {
   121  	return e.origin
   122  }
   123  
   124  func (e *Err) Http(w http.ResponseWriter) {
   125  	e.http.send(w)
   126  }
   127  
   128  func (e *Err) SetMessage(s string) *Err {
   129  	e.http.Message = s
   130  	return e
   131  }
   132  
   133  type err struct {
   134  	s string
   135  }
   136  
   137  func New(text string) *err {
   138  	return &err{text}
   139  }
   140  
   141  func (e *err) Error() string {
   142  	return e.s
   143  }
   144  
   145  func (e *err) Unauthorized(err ...error) *Err {
   146  	return &Err{
   147  		Code:   http.StatusText(http.StatusUnauthorized),
   148  		origin: getError(joinNameAndMessage(e.s, "access denied"), err...),
   149  		http:   HTTP.getUnauthorized(),
   150  	}
   151  }
   152  
   153  func (e *err) NotFound(err ...error) *Err {
   154  	return &Err{
   155  		Code:   http.StatusText(http.StatusNotFound),
   156  		origin: getError(joinNameAndMessage(e.s, "not found"), err...),
   157  		http:   HTTP.getNotFound(e.s),
   158  	}
   159  }
   160  
   161  func (e *err) InternalServerError(err ...error) *Err {
   162  	return &Err{
   163  		Code:   http.StatusText(http.StatusInternalServerError),
   164  		origin: getError(joinNameAndMessage(e.s, "internal server error"), err...),
   165  		http:   HTTP.getInternalServerError(e.s),
   166  	}
   167  }
   168  
   169  func (e *err) NotUnique(attr string, err ...error) *Err {
   170  	return &Err{
   171  		Code:   StatusNotUnique,
   172  		origin: getError(joinNameAndMessage(e.s, strings.ToLower(attr)+" not unique"), err...),
   173  		http:   HTTP.getNotUnique(strings.ToLower(attr)),
   174  	}
   175  }
   176  
   177  func (e *err) Allocated(attr string, err ...error) *Err {
   178  	return &Err{
   179  		Code:   StatusInUse,
   180  		Attr:   attr,
   181  		origin: getError(joinNameAndMessage(e.s, strings.ToLower(attr))+" is in use", err...),
   182  		http:   HTTP.getAllocatedParameter(strings.ToLower(attr)),
   183  	}
   184  }
   185  
   186  func (e *err) BadParameter(attr string, err ...error) *Err {
   187  	return &Err{
   188  		Code:   StatusBadParameter,
   189  		Attr:   attr,
   190  		origin: getError(joinNameAndMessage(e.s, "bad parameter "+strings.ToLower(attr)), err...),
   191  		http:   HTTP.getBadParameter(attr),
   192  	}
   193  }
   194  
   195  func (e *err) BadRequest(msg string, err ...error) *Err {
   196  	return &Err{
   197  		Code:   StatusBadParameter,
   198  		origin: getError(joinNameAndMessage(e.s, msg), err...),
   199  		http:   HTTP.getBadRequest(msg),
   200  	}
   201  }
   202  
   203  func (e *err) IncorrectJSON(err ...error) *Err {
   204  	return &Err{
   205  		Code:   StatusIncorrectJson,
   206  		origin: getError(joinNameAndMessage(e.s, "incorrect json"), err...),
   207  		http:   HTTP.getIncorrectJSON(),
   208  	}
   209  }
   210  
   211  func (e *err) IncorrectXML(err ...error) *Err {
   212  	return &Err{
   213  		Code:   StatusIncorrectJson,
   214  		origin: getError(joinNameAndMessage(e.s, "incorrect xml"), err...),
   215  		http:   HTTP.getIncorrectXML(),
   216  	}
   217  }
   218  
   219  func (e *err) Forbidden(err ...error) *Err {
   220  	return &Err{
   221  		Code:   StatusForbidden,
   222  		origin: getError(joinNameAndMessage(e.s, "forbidden"), err...),
   223  		http:   HTTP.getForbidden(),
   224  	}
   225  }
   226  
   227  func (e *err) NotAllowed(err ...error) *Err {
   228  	return &Err{
   229  		Code:   StatusNotAllowed,
   230  		origin: getError(joinNameAndMessage(e.s, "not allowed"), err...),
   231  		http:   HTTP.getNotAllowed(),
   232  	}
   233  }
   234  
   235  func (e *err) Unknown(err ...error) *Err {
   236  	return &Err{
   237  		Code:   StatusUnknown,
   238  		origin: getError(joinNameAndMessage(e.s, "unknown error"), err...),
   239  		http:   HTTP.getUnknown(),
   240  	}
   241  }
   242  
   243  func getError(msg string, err ...error) error {
   244  	if len(err) == 0 {
   245  		return errors.New(msg)
   246  	} else {
   247  		return err[0]
   248  	}
   249  }
   250  
   251  func joinNameAndMessage(name, message string) string {
   252  	return toUpperFirstChar(name) + ": " + message
   253  }
   254  
   255  func toUpperFirstChar(srt string) string {
   256  	return strings.ToUpper(srt[0:1]) + srt[1:]
   257  }