code: func ServeError(rw http.ResponseWriter, r *http.Request, err error) {
  	rw.Header().Set("Content-Type", "application/json")
  	switch e := err.(type) {
  	case *CompositeError:
  		er := flattenComposite(e)
  		// strips composite errors to first element only
  		if len(er.Errors) > 0 {
  			ServeError(rw, r, er.Errors[0])
  		} else {
  			// guard against empty CompositeError (invalid construct)
  			ServeError(rw, r, nil)
  		}
  	case *MethodNotAllowedError:
  		rw.Header().Add("Allow", strings.Join(err.(*MethodNotAllowedError).Allowed, ","))
  		rw.WriteHeader(asHTTPCode(int(e.Code())))
  		if r == nil || r.Method != http.MethodHead {
  			_, _ = rw.Write(errorAsJSON(e))
  		}
  	case Error:
  		value := reflect.ValueOf(e)
  		if value.Kind() == reflect.Ptr && value.IsNil() {
  			rw.WriteHeader(http.StatusInternalServerError)
  			_, _ = rw.Write(errorAsJSON(New(http.StatusInternalServerError, "Unknown error")))
  			return
  		}
  		rw.WriteHeader(asHTTPCode(int(e.Code())))
  		if r == nil || r.Method != http.MethodHead {
  			_, _ = rw.Write(errorAsJSON(e))
  		}
  	case nil:
  		rw.WriteHeader(http.StatusInternalServerError)
  		_, _ = rw.Write(errorAsJSON(New(http.StatusInternalServerError, "Unknown error")))
  	default:
  		rw.WriteHeader(http.StatusInternalServerError)
  		if r == nil || r.Method != http.MethodHead {
  			_, _ = rw.Write(errorAsJSON(New(http.StatusInternalServerError, err.Error())))
  		}
  	}
  }
docs: // ServeError the error handler interface implementation