github.com/mundipagg/boleto-api@v0.0.0-20230620145841-3f9ec742599f/models/errors.go (about)

     1  package models
     2  
     3  // IErrorResponse interface para implementar Error
     4  type IErrorResponse interface {
     5  	Error() string
     6  	ErrorCode() string
     7  }
     8  
     9  // DataError objeto de erro
    10  type ArrayDataError struct {
    11  	Error []ErrorResponse `json:"error"`
    12  }
    13  
    14  // ErrorResponse objeto de erro
    15  type ErrorResponse struct {
    16  	Code    string `json:"code,omitempty"`
    17  	Message string `json:"message,omitempty"`
    18  }
    19  
    20  //NewErrorResponse cria um novo objeto de ErrorReponse com código e mensagem
    21  func NewErrorResponse(code, msg string) ErrorResponse {
    22  	return ErrorResponse{Code: code, Message: msg}
    23  }
    24  
    25  // ErrorCode retorna código do erro
    26  func (e ErrorResponse) ErrorCode() string {
    27  	return e.Code
    28  }
    29  
    30  func (e ErrorResponse) Error() string {
    31  	return e.Message
    32  }
    33  
    34  // Errors coleção de erros
    35  type Errors []ErrorResponse
    36  
    37  // NewErrors cria nova coleção de erros vazia
    38  func NewErrors() Errors {
    39  	return []ErrorResponse{}
    40  }
    41  
    42  // NewErrorCollection cria nova coleção de erros
    43  func NewErrorCollection(errorResponse ErrorResponse) Errors {
    44  	return []ErrorResponse{errorResponse}
    45  }
    46  
    47  // NewSingleErrorCollection cria nova coleção de erros com 1 item
    48  func NewSingleErrorCollection(code, msg string) Errors {
    49  	return NewErrorCollection(NewErrorResponse(code, msg))
    50  }
    51  
    52  // GatewayTimeout objeto para erros 404 da aplicação: ex boleto não encontrado
    53  type GatewayTimeout ErrorResponse
    54  
    55  //NewGatewayTimeout cria um novo objeto NewGatewayTimeout a partir de uma mensagem original e final
    56  func NewGatewayTimeout(code, msg string) GatewayTimeout {
    57  	return GatewayTimeout{Message: msg, Code: code}
    58  }
    59  
    60  //ErrorCode ErrorCode
    61  func (e GatewayTimeout) ErrorCode() string {
    62  	return e.Code
    63  }
    64  
    65  //Error Error
    66  func (e GatewayTimeout) Error() string {
    67  	return e.Message
    68  }
    69  
    70  //InternalServerError IServerError interface para implementar Error
    71  type InternalServerError ErrorResponse
    72  
    73  //NewInternalServerError cria um novo objeto InternalServerError a partir de uma mensagem original e final
    74  func NewInternalServerError(code, msg string) InternalServerError {
    75  	return InternalServerError{Message: msg, Code: code}
    76  }
    77  
    78  //ErrorCode Message retorna a mensagem final para o usuário
    79  func (e InternalServerError) ErrorCode() string {
    80  	return e.Code
    81  }
    82  
    83  //Error retorna o erro original
    84  func (e InternalServerError) Error() string {
    85  	return e.Message
    86  }
    87  
    88  //HttpNotFound interface para implementar Error
    89  type HttpNotFound ErrorResponse
    90  
    91  //NewHTTPNotFound cria um novo objeto NewHttpNotFound a partir de uma mensagem original e final
    92  func NewHTTPNotFound(code, msg string) HttpNotFound {
    93  	return HttpNotFound{Message: msg, Code: code}
    94  }
    95  
    96  //ErrorCode Message retorna a mensagem final para o usuário
    97  func (e HttpNotFound) ErrorCode() string {
    98  	return e.Code
    99  }
   100  
   101  //Error retorna o erro original
   102  func (e HttpNotFound) Error() string {
   103  	return e.Message
   104  }
   105  
   106  //FormatError interface para implementar Error
   107  type FormatError ErrorResponse
   108  
   109  //NewFormatError cria um novo objeto de FormatError com descrição do erro
   110  func NewFormatError(e string) FormatError {
   111  	return FormatError{Message: e}
   112  }
   113  
   114  //Error Retorna um erro code
   115  func (e FormatError) Error() string {
   116  	return e.Message
   117  }
   118  
   119  //ErrorCode Retorna um erro code
   120  func (e FormatError) ErrorCode() string {
   121  	return e.Code
   122  }
   123  
   124  //BadGatewayError interface para implementar Error
   125  type BadGatewayError ErrorResponse
   126  
   127  //NewBadGatewayError cria um novo objeto de BadGatewayError com descrição do erro
   128  func NewBadGatewayError(e string) BadGatewayError {
   129  	return BadGatewayError{Message: e}
   130  }
   131  
   132  //Error Retorna um erro code
   133  func (e BadGatewayError) Error() string {
   134  	return e.Message
   135  }
   136  
   137  //ErrorCode Retorna um erro code
   138  func (e BadGatewayError) ErrorCode() string {
   139  	return e.Code
   140  }
   141  
   142  //Append adiciona mais um erro na coleção
   143  func (e *Errors) Append(code, message string) {
   144  	*e = append(*e, ErrorResponse{Code: code, Message: message})
   145  }