github.com/koko1123/flow-go-1@v0.29.6/engine/access/rest/error.go (about)

     1  package rest
     2  
     3  import "net/http"
     4  
     5  // StatusError provides custom error with http status.
     6  type StatusError interface {
     7  	error                // this is the actual error that occured
     8  	Status() int         // the HTTP status code to return
     9  	UserMessage() string // the error message to return to the client
    10  }
    11  
    12  // NewRestError creates an error returned to user with provided status
    13  // user displayed message and internal error
    14  func NewRestError(status int, msg string, err error) *Error {
    15  	return &Error{
    16  		status:      status,
    17  		userMessage: msg,
    18  		err:         err,
    19  	}
    20  }
    21  
    22  // NewNotFoundError creates a new not found rest error.
    23  func NewNotFoundError(msg string, err error) *Error {
    24  	return &Error{
    25  		status:      http.StatusNotFound,
    26  		userMessage: msg,
    27  		err:         err,
    28  	}
    29  }
    30  
    31  // NewBadRequestError creates a new bad request rest error.
    32  func NewBadRequestError(err error) *Error {
    33  	return &Error{
    34  		status:      http.StatusBadRequest,
    35  		userMessage: err.Error(),
    36  		err:         err,
    37  	}
    38  }
    39  
    40  // Error is implementation of status error.
    41  type Error struct {
    42  	status      int
    43  	userMessage string
    44  	err         error
    45  }
    46  
    47  func (e *Error) UserMessage() string {
    48  	return e.userMessage
    49  }
    50  
    51  // Status returns error http status code.
    52  func (e *Error) Status() int {
    53  	return e.status
    54  }
    55  
    56  func (e *Error) Error() string {
    57  	return e.err.Error()
    58  }