func (s *ErrorsSuite) TestErrorCause(c *gc.C) { rootCause := errors.NewResourceNotFoundf(nil, "some value", "") // Construct a new error, based on a resource not found root cause. err := errors.Newf(rootCause, "an error occurred") c.Assert(err.Cause(), gc.Equals, rootCause) // Check the other error attributes. c.Assert(err.Error(), gc.Equals, "an error occurred\ncaused by: Resource Not Found: some value") }
// The HTTP response status code was not one of those expected, so we construct an error. // NotFound (404) codes have their own NotFound error type. // We also make a guess at duplicate value errors. func handleError(URL string, resp *http.Response) error { errBytes, _ := ioutil.ReadAll(resp.Body) errInfo := string(errBytes) // Check if we have a JSON representation of the failure, if so decode it. if resp.Header.Get("Content-Type") == contentTypeJSON { var errResponse ErrorResponse if err := json.Unmarshal(errBytes, &errResponse); err == nil { errInfo = errResponse.Message } } httpError := &HttpError{ resp.StatusCode, map[string][]string(resp.Header), URL, errInfo, } switch resp.StatusCode { case http.StatusBadRequest: return errors.NewBadRequestf(httpError, "", "Bad request %s", URL) case http.StatusUnauthorized: return errors.NewNotAuthorizedf(httpError, "", "Unauthorised URL %s", URL) //return errors.NewInvalidCredentialsf(httpError, "", "Unauthorised URL %s", URL) case http.StatusForbidden: //return errors. case http.StatusNotFound: return errors.NewResourceNotFoundf(httpError, "", "Resource not found %s", URL) case http.StatusMethodNotAllowed: //return errors. case http.StatusNotAcceptable: return errors.NewInvalidHeaderf(httpError, "", "Invalid Header %s", URL) case http.StatusConflict: return errors.NewMissingParameterf(httpError, "", "Missing parameters %s", URL) //return errors.NewInvalidArgumentf(httpError, "", "Invalid parameter %s", URL) case http.StatusRequestEntityTooLarge: return errors.NewRequestTooLargef(httpError, "", "Request too large %s", URL) case http.StatusUnsupportedMediaType: //return errors. case http.StatusServiceUnavailable: return errors.NewInternalErrorf(httpError, "", "Internal error %s", URL) case 420: // SlowDown return errors.NewRequestThrottledf(httpError, "", "Request throttled %s", URL) case 422: // Unprocessable Entity return errors.NewInvalidArgumentf(httpError, "", "Invalid parameters %s", URL) case 449: // RetryWith return errors.NewInvalidVersionf(httpError, "", "Invalid version %s", URL) //RequestMovedError -> ? } return errors.NewUnknownErrorf(httpError, "", "Unknown error %s", URL) }
func (s *ErrorsSuite) TestCreateResourceNotFoundError(c *gc.C) { context := "context" err := errors.NewResourceNotFoundf(nil, context, "It was resource not found: %s", context) c.Assert(errors.IsResourceNotFound(err), gc.Equals, true) c.Assert(err.Error(), gc.Equals, "It was resource not found: context") }
func (s *ErrorsSuite) TestCreateSimpleResourceNotFoundError(c *gc.C) { context := "context" err := errors.NewResourceNotFoundf(nil, context, "") c.Assert(errors.IsResourceNotFound(err), gc.Equals, true) c.Assert(err.Error(), gc.Equals, "Resource Not Found: context") }