error.go 850 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package authority
  2. import (
  3. "net/http"
  4. )
  5. type context map[string]interface{}
  6. // Error implements the api.Error interface and adds context to error messages.
  7. type apiError struct {
  8. err error
  9. code int
  10. context context
  11. }
  12. // Cause implements the errors.Causer interface and returns the original error.
  13. func (e *apiError) Cause() error {
  14. return e.err
  15. }
  16. // Error returns an error message with additional context.
  17. func (e *apiError) Error() string {
  18. ret := e.err.Error()
  19. /*
  20. if len(e.context) > 0 {
  21. ret += "\n\nContext:"
  22. for k, v := range e.context {
  23. ret += fmt.Sprintf("\n %s: %v", k, v)
  24. }
  25. }
  26. */
  27. return ret
  28. }
  29. // StatusCode returns an http status code indicating the type and severity of
  30. // the error.
  31. func (e *apiError) StatusCode() int {
  32. if e.code == 0 {
  33. return http.StatusInternalServerError
  34. }
  35. return e.code
  36. }