github.com/dhax/go-base@v0.0.0-20231004214136-8be7e5c1972b/auth/authorize/errors.go (about)

     1  package authorize
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/go-chi/render"
     7  )
     8  
     9  // ErrResponse renderer type for handling all sorts of errors.
    10  type ErrResponse struct {
    11  	Err            error `json:"-"` // low-level runtime error
    12  	HTTPStatusCode int   `json:"-"` // http response status code
    13  
    14  	StatusText string `json:"status"`          // user-level status message
    15  	AppCode    int64  `json:"code,omitempty"`  // application-specific error code
    16  	ErrorText  string `json:"error,omitempty"` // application-level error message, for debugging
    17  }
    18  
    19  // Render sets the application-specific error code in AppCode.
    20  func (e *ErrResponse) Render(w http.ResponseWriter, r *http.Request) error {
    21  	render.Status(r, e.HTTPStatusCode)
    22  	return nil
    23  }
    24  
    25  // The list of default error types without specific error message.
    26  var (
    27  	ErrForbidden = &ErrResponse{
    28  		HTTPStatusCode: http.StatusForbidden,
    29  		StatusText:     http.StatusText(http.StatusForbidden),
    30  	}
    31  )