gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/errors/errors.go (about)

     1  // Package errors provides a way to return detailed information
     2  // for an RPC request error. The error is normally JSON encoded.
     3  package errors
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"net/http"
     9  )
    10  
    11  // Error implements the error interface.
    12  type Error struct {
    13  	Id     string `json:"id"`
    14  	Code   int32  `json:"code"`
    15  	Detail string `json:"detail"`
    16  	Status string `json:"status"`
    17  }
    18  
    19  func (e *Error) Error() string {
    20  	b, _ := json.Marshal(e)
    21  	return string(b)
    22  }
    23  
    24  // New generates a custom error.
    25  func New(id, detail string, code int32) error {
    26  	return &Error{
    27  		Id:     id,
    28  		Code:   code,
    29  		Detail: detail,
    30  		Status: http.StatusText(int(code)),
    31  	}
    32  }
    33  
    34  // Parse tries to parse a JSON string into an error. If that
    35  // fails, it will set the given string as the error detail.
    36  func Parse(err string) *Error {
    37  	e := new(Error)
    38  	errr := json.Unmarshal([]byte(err), e)
    39  	if errr != nil {
    40  		e.Detail = err
    41  	}
    42  	return e
    43  }
    44  
    45  // BadRequest generates a 400 error.
    46  func BadRequest(id, format string, a ...interface{}) error {
    47  	return &Error{
    48  		Id:     id,
    49  		Code:   400,
    50  		Detail: fmt.Sprintf(format, a...),
    51  		Status: http.StatusText(400),
    52  	}
    53  }
    54  
    55  // Unauthorized generates a 401 error.
    56  func Unauthorized(id, format string, a ...interface{}) error {
    57  	return &Error{
    58  		Id:     id,
    59  		Code:   401,
    60  		Detail: fmt.Sprintf(format, a...),
    61  		Status: http.StatusText(401),
    62  	}
    63  }
    64  
    65  // Forbidden generates a 403 error.
    66  func Forbidden(id, format string, a ...interface{}) error {
    67  	return &Error{
    68  		Id:     id,
    69  		Code:   403,
    70  		Detail: fmt.Sprintf(format, a...),
    71  		Status: http.StatusText(403),
    72  	}
    73  }
    74  
    75  // NotFound generates a 404 error.
    76  func NotFound(id, format string, a ...interface{}) error {
    77  	return &Error{
    78  		Id:     id,
    79  		Code:   404,
    80  		Detail: fmt.Sprintf(format, a...),
    81  		Status: http.StatusText(404),
    82  	}
    83  }
    84  
    85  // MethodNotAllowed generates a 405 error.
    86  func MethodNotAllowed(id, format string, a ...interface{}) error {
    87  	return &Error{
    88  		Id:     id,
    89  		Code:   405,
    90  		Detail: fmt.Sprintf(format, a...),
    91  		Status: http.StatusText(405),
    92  	}
    93  }
    94  
    95  // Timeout generates a 408 error.
    96  func Timeout(id, format string, a ...interface{}) error {
    97  	return &Error{
    98  		Id:     id,
    99  		Code:   408,
   100  		Detail: fmt.Sprintf(format, a...),
   101  		Status: http.StatusText(408),
   102  	}
   103  }
   104  
   105  // Conflict generates a 409 error.
   106  func Conflict(id, format string, a ...interface{}) error {
   107  	return &Error{
   108  		Id:     id,
   109  		Code:   409,
   110  		Detail: fmt.Sprintf(format, a...),
   111  		Status: http.StatusText(409),
   112  	}
   113  }
   114  
   115  // InternalServerError generates a 500 error.
   116  func InternalServerError(id, format string, a ...interface{}) error {
   117  	return &Error{
   118  		Id:     id,
   119  		Code:   500,
   120  		Detail: fmt.Sprintf(format, a...),
   121  		Status: http.StatusText(500),
   122  	}
   123  }