github.com/diamondburned/arikawa/v2@v2.1.0/utils/httputil/errors.go (about)

     1  package httputil
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  )
     7  
     8  type JSONError struct {
     9  	err error
    10  }
    11  
    12  func (j JSONError) Error() string {
    13  	return "JSON decoding failed: " + j.err.Error()
    14  }
    15  
    16  func (j JSONError) Unwrap() error {
    17  	return j.err
    18  }
    19  
    20  type RequestError struct {
    21  	err error
    22  }
    23  
    24  func (r RequestError) Error() string {
    25  	return "request failed: " + r.err.Error()
    26  }
    27  
    28  func (r RequestError) Unwrap() error {
    29  	return r.err
    30  }
    31  
    32  type HTTPError struct {
    33  	Status int    `json:"-"`
    34  	Body   []byte `json:"-"`
    35  
    36  	Code    ErrorCode `json:"code"`
    37  	Message string    `json:"message,omitempty"`
    38  }
    39  
    40  func (err HTTPError) Error() string {
    41  	switch {
    42  	case err.Message != "":
    43  		return fmt.Sprintf("Discord %d error: %s", err.Status, err.Message)
    44  
    45  	case err.Code > 0:
    46  		return fmt.Sprintf("Discord returned status %d error code %d",
    47  			err.Status, err.Code)
    48  
    49  	case len(err.Body) > 0:
    50  		return fmt.Sprintf("Discord returned status %d body %s",
    51  			err.Status, string(err.Body))
    52  
    53  	default:
    54  		return "Discord returned status " + strconv.Itoa(err.Status)
    55  	}
    56  }
    57  
    58  type ErrorCode uint