// checkResponseCode can be used to check the status code of an HTTP request.
checkResponseCode can be used to check the status code of an HTTP request.
// checkResponseCode can be used to check the status code of an HTTP request .
func checkResponseCode(r *http.Response) error {
	if r.StatusCode >= 200 && r.StatusCode <= 299 {
		return nil
	}
	switch r.StatusCode {
	case 401:
		return ErrUnauthorized
	case 404:
		return ErrResourceNotFound
	case 409:
		switch {
		case strings.HasSuffix(r.Request.URL.Path, "actions/lock"):
			return ErrWorkspaceLocked
		case strings.HasSuffix(r.Request.URL.Path, "actions/unlock"):
			return ErrWorkspaceNotLocked
		case strings.HasSuffix(r.Request.URL.Path, "actions/force-unlock"):
			return ErrWorkspaceNotLocked
		}
	}
	errPayload := &jsonapi.ErrorsPayload{}
	err := json.NewDecoder(r.Body).Decode(errPayload)
	if err != nil || len(errPayload.Errors) == 0 {
		return fmt.Errorf(r.Status)
	}
	var errs []string
	for _, e := range errPayload.Errors {
		if e.Detail == "" {
			errs = append(errs, e.Title)
		} else {
			errs = append(errs, fmt.Sprintf("%s\n\n%s", e.Title, e.Detail))
		}
	}
	return fmt.Errorf(strings.Join(errs, "\n"))
}
func checkResponseCode ( r * http . Response ) error { if r . StatusCode >= 200 && r . StatusCode <= 299 { return nil } switch r . StatusCode { case 401 : return ErrUnauthorized case 404 : return ErrResourceNotFound case 409 : switch { case strings . HasSuffix ( r . Request . URL . Path , " " ) : return ErrWorkspaceLocked case strings . HasSuffix ( r . Request . URL . Path , " " ) : return ErrWorkspaceNotLocked case strings . HasSuffix ( r . Request . URL . Path , " " ) : return ErrWorkspaceNotLocked } } errPayload := & jsonapi . ErrorsPayload { } err := json . NewDecoder ( r . Body ) . Decode ( errPayload ) if err != nil || len ( errPayload . Errors ) == 0 { return fmt . Errorf ( r . Status ) } var errs [ ] string for _ , e := range errPayload . Errors { if e . Detail == " " { errs = append ( errs , e . Title ) } else { errs = append ( errs , fmt . Sprintf ( " \n \n " , e . Title , e . Detail ) ) } } return fmt . Errorf ( strings . Join ( errs , " \n " ) ) }
checkResponseCode can be used to check the status code of an HTTP request.
checkResponseCode can be used to check the status code of an HTTP request .
// Decode the error payload. // Parse and format the errors.