github.com/prysmaticlabs/prysm@v1.4.4/shared/gateway/api_middleware_structs.go (about)

     1  package gateway
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/pkg/errors"
     7  )
     8  
     9  // ---------------
    10  // Error handling.
    11  // ---------------
    12  
    13  // ErrorJson describes common functionality of all JSON error representations.
    14  type ErrorJson interface {
    15  	StatusCode() int
    16  	SetCode(code int)
    17  	Msg() string
    18  }
    19  
    20  // DefaultErrorJson is a JSON representation of a simple error value, containing only a message and an error code.
    21  type DefaultErrorJson struct {
    22  	Message string `json:"message"`
    23  	Code    int    `json:"code"`
    24  }
    25  
    26  // InternalServerErrorWithMessage returns a DefaultErrorJson with 500 code and a custom message.
    27  func InternalServerErrorWithMessage(err error, message string) *DefaultErrorJson {
    28  	e := errors.Wrapf(err, message)
    29  	return &DefaultErrorJson{
    30  		Message: e.Error(),
    31  		Code:    http.StatusInternalServerError,
    32  	}
    33  }
    34  
    35  // InternalServerError returns a DefaultErrorJson with 500 code.
    36  func InternalServerError(err error) *DefaultErrorJson {
    37  	return &DefaultErrorJson{
    38  		Message: err.Error(),
    39  		Code:    http.StatusInternalServerError,
    40  	}
    41  }
    42  
    43  // StatusCode returns the error's underlying error code.
    44  func (e *DefaultErrorJson) StatusCode() int {
    45  	return e.Code
    46  }
    47  
    48  // Msg returns the error's underlying message.
    49  func (e *DefaultErrorJson) Msg() string {
    50  	return e.Message
    51  }
    52  
    53  // SetCode sets the error's underlying error code.
    54  func (e *DefaultErrorJson) SetCode(code int) {
    55  	e.Code = code
    56  }