github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/api/errors/errors.go (about) 1 package errors 2 3 import "net/http" 4 5 // apiError is an error wrapper that also 6 // holds information about response status codes. 7 type apiError struct { 8 error 9 statusCode int 10 } 11 12 // HTTPErrorStatusCode returns a status code. 13 func (e apiError) HTTPErrorStatusCode() int { 14 return e.statusCode 15 } 16 17 // NewErrorWithStatusCode allows you to associate 18 // a specific HTTP Status Code to an error. 19 // The Server will take that code and set 20 // it as the response status. 21 func NewErrorWithStatusCode(err error, code int) error { 22 return apiError{err, code} 23 } 24 25 // NewBadRequestError creates a new API error 26 // that has the 400 HTTP status code associated to it. 27 func NewBadRequestError(err error) error { 28 return NewErrorWithStatusCode(err, http.StatusBadRequest) 29 } 30 31 // NewRequestForbiddenError creates a new API error 32 // that has the 403 HTTP status code associated to it. 33 func NewRequestForbiddenError(err error) error { 34 return NewErrorWithStatusCode(err, http.StatusForbidden) 35 } 36 37 // NewRequestNotFoundError creates a new API error 38 // that has the 404 HTTP status code associated to it. 39 func NewRequestNotFoundError(err error) error { 40 return NewErrorWithStatusCode(err, http.StatusNotFound) 41 } 42 43 // NewRequestConflictError creates a new API error 44 // that has the 409 HTTP status code associated to it. 45 func NewRequestConflictError(err error) error { 46 return NewErrorWithStatusCode(err, http.StatusConflict) 47 }