// NewAPIError creates a new API error based on a Response,
// or http.Response-like.
542998
func NewAPIError(response *http.Response) APIError {
	// TODO: handle this error
	body, _ := ioutil.ReadAll(response.Body)

	return NewAPIErrorFromBody(response, body)
}
252502
func NewAPIError(opName string, payload interface{}, code int) *APIError {
	return &APIError{
		OperationName: opName,
		Response:      payload,
		Code:          code,
	}
}
1116915
func (api *API) RespondError(err error) error {
	var response errorResponse
	response.Error = err.Error()

	return sendSerializedJSONMessage(response, api.Writer)
}
127613
func HandleError(h httpx.Handler, f ErrorHandlerFunc) *Error {
	e := NewError(h)
	e.ErrorHandler = f
	return e
}
227937
func NewHttpErrorf(err error, status int, format string, a ...interface{}) *HttpError {
	if status < 1 {
		status = 500
	}
	return &HttpError{status, NewErrorf(err, format, a...)}
}
367488
func newError(StatusCode int, Text string) *Error {
	return &Error{
		StatusCode: StatusCode,
		Text:       Text,
	}
}
23391
func NewQuietError(req *http.Request, code int32, e error) error {
	return errors.Wrap(&Error{
		silent:   true,
		redirect: false,
		source:   req.URL.String(),
		request:  req,
		err:      newAPIError(code, errString(e)),
	}, 1)
}
422
func (apigError *Error) Error() string {
	bytes, bytesErr := json.Marshal(apigError)
	if bytesErr != nil {
		bytes = []byte(http.StatusText(http.StatusInternalServerError))
	}
	return string(bytes)
}
72188
func NewCliError(reqType string, hr *gocd.APIResponse, err error) (jerr JSONCliError) {
	data := dataJSONCliError{
		"error": err.Error(),
	}
	if hr != nil {
		data["status"] = hr.HTTP.StatusCode
		data["response-body"] = hr.Body
		data["request-endpoint"] = hr.Request.HTTP.URL.String()

		if hr.HTTP.StatusCode == 404 {
			data["error"] = "Resource not found"
		} else {
			b1, _ := json.Marshal(hr.HTTP.Header)
			b2, _ := json.Marshal(hr.Request.HTTP.Header)
			data["error"] = "An error occurred while retrieving the resource."
			data["response-header"] = string(b1)
			data["request-body"] = hr.Request.Body
			data["request-header"] = string(b2)
		}
	} else {
		data["request"] = reqType
	}
	return JSONCliError{
		data: data,
		resp: hr,
	}
}
92940
func NewCLIError(msg string, args ...interface{}) error {
	return CommandLineError{fmt.Errorf(msg, args...)}
}
920579
func NewError(options ...func(*Error)) *Error {
	err := &Error{}
	for _, o := range options {
		o(err)
	}

	return err
}
827708
func newError(err error, msg string) *Error {
	return &Error{
		Message: msg,
		Err:     err,
	}
}
198444
func (d *Document) AddError(newErr *Error) *Error {

	if d.HasData() {
		return ISE("Attempting to set an error, when the document has prepared response data")
	}

	if newErr.Status == 0 {
		return ISE("No HTTP Status code provided for error, cannot add to document")
	}

	if d.Status == 0 {
		d.Status = newErr.Status
	}

	if d.Errors == nil {
		d.Errors = []*Error{newErr}
	} else {
		d.Errors = append(d.Errors, newErr)
	}

	// set document to error mode
	d.Mode = ErrorMode

	return nil
}
465170
func httpError(status int, format string, a ...interface{}) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, fmt.Sprintf(format, a...), http.StatusInternalServerError)
	})
}
690043
func NewResponseError(resp *http.Response) error {
	if resp.StatusCode >= 200 && resp.StatusCode < 400 {
		return nil
	}

	body, err := ReadResponse(resp)
	if err != nil {
		return err
	}

	return errutil.New(body)
}
692014
func newError(err ResultError, context *JsonContext, value interface{}, locale locale, details ErrorDetails) {
	var t string
	var d string
	switch err.(type) {
	case *RequiredError:
		t = "required"
		d = locale.Required()
	case *InvalidTypeError:
		t = "invalid_type"
		d = locale.InvalidType()
	case *NumberAnyOfError:
		t = "number_any_of"
		d = locale.NumberAnyOf()
	case *NumberOneOfError:
		t = "number_one_of"
		d = locale.NumberOneOf()
	case *NumberAllOfError:
		t = "number_all_of"
		d = locale.NumberAllOf()
	case *NumberNotError:
		t = "number_not"
		d = locale.NumberNot()
	case *MissingDependencyError:
		t = "missing_dependency"
		d = locale.MissingDependency()
	case *InternalError:
		t = "internal"
		d = locale.Internal()
	case *ConstError:
		t = "const"
		d = locale.Const()
	case *EnumError:
		t = "enum"
		d = locale.Enum()
	case *ArrayNoAdditionalItemsError:
		t = "array_no_additional_items"
		d = locale.ArrayNoAdditionalItems()
	case *ArrayMinItemsError:
		t = "array_min_items"
		d = locale.ArrayMinItems()
	case *ArrayMaxItemsError:
		t = "array_max_items"
		d = locale.ArrayMaxItems()
	case *ItemsMustBeUniqueError:
		t = "unique"
		d = locale.Unique()
	case *ArrayContainsError:
		t = "contains"
		d = locale.ArrayContains()
	case *ArrayMinPropertiesError:
		t = "array_min_properties"
		d = locale.ArrayMinProperties()
	case *ArrayMaxPropertiesError:
		t = "array_max_properties"
		d = locale.ArrayMaxProperties()
	case *AdditionalPropertyNotAllowedError:
		t = "additional_property_not_allowed"
		d = locale.AdditionalPropertyNotAllowed()
	case *InvalidPropertyPatternError:
		t = "invalid_property_pattern"
		d = locale.InvalidPropertyPattern()
	case *InvalidPropertyNameError:
		t = "invalid_property_name"
		d = locale.InvalidPropertyName()
	case *StringLengthGTEError:
		t = "string_gte"
		d = locale.StringGTE()
	case *StringLengthLTEError:
		t = "string_lte"
		d = locale.StringLTE()
	case *DoesNotMatchPatternError:
		t = "pattern"
		d = locale.DoesNotMatchPattern()
	case *DoesNotMatchFormatError:
		t = "format"
		d = locale.DoesNotMatchFormat()
	case *MultipleOfError:
		t = "multiple_of"
		d = locale.MultipleOf()
	case *NumberGTEError:
		t = "number_gte"
		d = locale.NumberGTE()
	case *NumberGTError:
		t = "number_gt"
		d = locale.NumberGT()
	case *NumberLTEError:
		t = "number_lte"
		d = locale.NumberLTE()
	case *NumberLTError:
		t = "number_lt"
		d = locale.NumberLT()
	case *ConditionThenError:
		t = "condition_then"
		d = locale.ConditionThen()
	case *ConditionElseError:
		t = "condition_else"
		d = locale.ConditionElse()
	}

	err.SetType(t)
	err.SetContext(context)
	err.SetValue(value)
	err.SetDetails(details)
	err.SetDescriptionFormat(d)
	details["field"] = err.Field()

	if _, exists := details["context"]; !exists && context != nil {
		details["context"] = context.String()
	}

	err.SetDescription(formatErrorDescription(err.DescriptionFormat(), details))
}
863273
func NewFailOnError(authRequestHandlers ...authenticator.Request) authenticator.Request {
	if len(authRequestHandlers) == 1 {
		return authRequestHandlers[0]
	}
	return &unionAuthRequestHandler{Handlers: authRequestHandlers, FailOnError: true}
}