// createResponseError creates an Error structure from the HTTP response,
// its status code and the error information sent by Elasticsearch.
518990
func createResponseError(res *http.Response) error {
	if res.Body == nil {
		return &Error{Status: res.StatusCode}
	}
	data, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return &Error{Status: res.StatusCode}
	}
	errReply := new(Error)
	err = json.Unmarshal(data, errReply)
	if err != nil {
		return &Error{Status: res.StatusCode}
	}
	if errReply != nil {
		if errReply.Status == 0 {
			errReply.Status = res.StatusCode
		}
		return errReply
	}
	return &Error{Status: res.StatusCode}
}
186261
func (r *Request) newUnstructuredResponseError(body []byte, isTextResponse bool, statusCode int, method string, retryAfter int) error {
	// cap the amount of output we create
	if len(body) > maxUnstructuredResponseTextBytes {
		body = body[:maxUnstructuredResponseTextBytes]
	}

	message := "unknown"
	if isTextResponse {
		message = strings.TrimSpace(string(body))
	}
	var groupResource schema.GroupResource
	if len(r.resource) > 0 {
		groupResource.Group = r.content.GroupVersion.Group
		groupResource.Resource = r.resource
	}
	return errors.NewGenericServerResponse(
		statusCode,
		method,
		groupResource,
		r.resourceName,
		message,
		retryAfter,
		true,
	)
}
47546
func createResponse(req *http.Request) *http.Response {
	return &http.Response{
		ProtoMajor: 1,
		ProtoMinor: 1,
		Proto:      "HTTP/1.1",
		Request:    req,
		Header:     make(http.Header),
		Body:       createReadCloser([]byte{}),
	}
}
1111866
func NewFailureResponseBuilder(err error, statusCode int, loggerAction string) *FailureResponseBuilder {
	return &FailureResponseBuilder{
		error:         err,
		statusCode:    statusCode,
		loggerAction:  loggerAction,
		emptyResponse: false,
	}
}
381783
func NewError(responseCode int, err *sacloud.ResultErrorValue) Error {
	return &apiError{
		responseCode: responseCode,
		origErr:      err,
	}
}
1355795
func ErrorResponseHandler(request *restful.Request, response *restful.Response, origin error, err *ErrorResponse) {
	fields := log.Fields{
		"Internal":    err.Err[0].InternalError,
		"Description": err.Err[0].Msg,
		"Context":     err.Err[0].Context,
		"URL":         request.Request.URL.String(),
		"Method":      request.Request.Method,
	}

	for k, vs := range request.Request.Form {
		var out string
		for i, v := range vs {
			if i > 0 {
				out += ","
			}
			out += v
		}
		fields[k] = out
	}
	log.WithFields(fields).Error(origin)

	if response != nil {
		response.WriteHeaderAndEntity(err.Err[0].Code, err)
	}
}
382851
func DefaultErrorParser(resp *http.Response) error {
	resBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	rerr := new(Error)
	err = json.Unmarshal(resBody, rerr)
	if err != nil {
		return fmt.Errorf("invalid response body: %s", string(resBody))
	}
	if rerr.Title == "" {
		return fmt.Errorf("invalid response body: %s", string(resBody))
	} else {
		rerr.Status = resp.StatusCode
		return rerr
	}
}
161295
func writeSTSErrorResponse(w http.ResponseWriter, err STSError) {
	// Generate error response.
	stsErrorResponse := getSTSErrorResponse(err, w.Header().Get(responseRequestIDKey))
	encodedErrorResponse := encodeResponse(stsErrorResponse)
	writeResponse(w, err.HTTPStatusCode, encodedErrorResponse, mimeXML)
}
1049290
func encodeResponseErr(w http.ResponseWriter, err interface{}) {
	enc := json.NewEncoder(w)

	rErr, ok := err.(*apitypes.Error)
	if ok {
		w.WriteHeader(rErr.StatusCode())
		enc.Encode(rErr)
	} else {
		w.WriteHeader(http.StatusInternalServerError)
		enc.Encode(&errorMsg{
			Type:  apitypes.InternalServerError,
			Error: []string{"Internal server error"},
		})
	}
}
542998
func NewAPIError(response *http.Response) APIError {
	// TODO: handle this error
	body, _ := ioutil.ReadAll(response.Body)

	return NewAPIErrorFromBody(response, body)
}
1171109
func ParseError(response *http.Response) error {
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return err
	}
	awsErr := new(Error)
	err = json.Unmarshal(body, awsErr)
	if err != nil {
		return err
	}
	return awsErr
}
1321673
func createResponse(req *http.Request) *http.Response {
	return &http.Response{
		ProtoMajor: 1,
		ProtoMinor: 1,
		Proto:      "HTTP/1.1",
		Request:    req,
		Header:     make(http.Header),
		Body:       utils.NopCloser(),
	}
}
745945
func SolrErrorResponse(m map[string]interface{}) (bool, *ErrorResponse) {
	// check for existance of "error" key
	if _, found := m["error"]; found {
		error := m["error"].(map[string]interface{})
		return true, &ErrorResponse{
			Message: error["msg"].(string),
			Status:  int(error["code"].(float64)),
		}
	}
	return false, nil
}
1348469
func JSONErrorResponse(ctx InternalServerError, err error) error {
	jsonErr, status := ErrorToJSONAPIErrors(ctx, err)
	switch status {
	case http.StatusBadRequest:
		if ctx, ok := ctx.(BadRequest); ok {
			return errs.WithStack(ctx.BadRequest(jsonErr))
		}
	case http.StatusNotFound:
		if ctx, ok := ctx.(NotFound); ok {
			return errs.WithStack(ctx.NotFound(jsonErr))
		}
	case http.StatusUnauthorized:
		if ctx, ok := ctx.(Unauthorized); ok {
			return errs.WithStack(ctx.Unauthorized(jsonErr))
		}
	case http.StatusForbidden:
		if ctx, ok := ctx.(Forbidden); ok {
			return errs.WithStack(ctx.Forbidden(jsonErr))
		}
	case http.StatusConflict:
		if ctx, ok := ctx.(Conflict); ok {
			return errs.WithStack(ctx.Conflict(jsonErr))
		}
	}

	sentry.Sentry().CaptureError(ctx, err)
	return errs.WithStack(ctx.InternalServerError(jsonErr))
}
934241
func checkResponseError(resp *http.Response) error {
	if resp.StatusCode < 200 || resp.StatusCode >= 400 {
		var mailjetErr RequestError
		err := json.NewDecoder(resp.Body).Decode(&mailjetErr)

		if err != nil {
			return fmt.Errorf("Unexpected server response code: %d: %s", resp.StatusCode, err)
		}
		return fmt.Errorf("Unexpected server response code: %d: %s (%s)",
			resp.StatusCode, mailjetErr.ErrorMessage, mailjetErr.ErrorInfo)
	}
	return nil
}
1230651
func CreateProposalResponse(hdrbytes []byte, payl []byte, response *peer.Response, results []byte, events []byte, ccid *peer.ChaincodeID, visibility []byte, signingEndorser msp.SigningIdentity) (*peer.ProposalResponse, error) {
	hdr, err := GetHeader(hdrbytes)
	if err != nil {
		return nil, err
	}

	// obtain the proposal hash given proposal header, payload and the
	// requested visibility
	pHashBytes, err := GetProposalHash1(hdr, payl, visibility)
	if err != nil {
		return nil, errors.WithMessage(err, "error computing proposal hash")
	}

	// get the bytes of the proposal response payload - we need to sign them
	prpBytes, err := GetBytesProposalResponsePayload(pHashBytes, response, results, events, ccid)
	if err != nil {
		return nil, err
	}

	// serialize the signing identity
	endorser, err := signingEndorser.Serialize()
	if err != nil {
		return nil, errors.WithMessage(err, fmt.Sprintf("error serializing signing identity for %s", signingEndorser.GetIdentifier()))
	}

	// sign the concatenation of the proposal response and the serialized
	// endorser identity with this endorser's key
	signature, err := signingEndorser.Sign(append(prpBytes, endorser...))
	if err != nil {
		return nil, errors.WithMessage(err, "could not sign the proposal response payload")
	}

	resp := &peer.ProposalResponse{
		// Timestamp: TODO!
		Version: 1, // TODO: pick right version number
		Endorsement: &peer.Endorsement{
			Signature: signature,
			Endorser:  endorser,
		},
		Payload: prpBytes,
		Response: &peer.Response{
			Status:  200,
			Message: "OK",
		},
	}

	return resp, nil
}
428065
func newFutureError(err error) chan *response {
	responseChan := make(chan *response, 1)
	responseChan <- &response{err: err}
	return responseChan
}