Esempio n. 1
0
func (s *ErrorsSuite) TestErrorIsType(c *gc.C) {
	rootCause := errors.NewBadRequestf(nil, "some value", "")
	// Construct a new error, based on a bad request root cause.
	err := errors.Newf(rootCause, "an error occurred")
	// Check that the error is not falsely identified as something it is not.
	c.Assert(errors.IsNotAuthorized(err), gc.Equals, false)
	// Check that the error is correctly identified as a not found error.
	c.Assert(errors.IsBadRequest(err), gc.Equals, true)
}
Esempio n. 2
0
// 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)
}
Esempio n. 3
0
func (s *ErrorsSuite) TestCreateBadRequestError(c *gc.C) {
	context := "context"
	err := errors.NewBadRequestf(nil, context, "It was bad request: %s", context)
	c.Assert(errors.IsBadRequest(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "It was bad request: context")
}