1
0
mirror of synced 2024-11-22 04:56:06 +03:00
mg-transport-api-client-go/v1/errors.go
Ruslan Efanov 5e3e7cd03d fix review
2022-10-26 18:21:00 +03:00

80 lines
1.4 KiB
Go

package v1
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
)
var defaultErrorMessage = "http client error"
var internalServerError = "internal server error"
var marshalError = "cannot unmarshal response body"
type MGErrors struct {
Errors []string
}
type httpClientError struct {
ErrorMsg string
BaseError error
Response io.Reader
}
func (err *httpClientError) Unwrap() error {
return err.BaseError
}
func (err *httpClientError) Error() string {
message := defaultErrorMessage
if err.BaseError != nil {
message = fmt.Sprintf("%s: %s", defaultErrorMessage, err.BaseError.Error())
}
if len(err.ErrorMsg) > 0 {
message = err.ErrorMsg
}
return message
}
func NewCriticalHTTPError(err error) error {
return &httpClientError{BaseError: err}
}
func NewAPIClientError(responseBody []byte) error {
var data MGErrors
var message string
if len(responseBody) == 0 {
message = internalServerError
} else {
err := json.Unmarshal(responseBody, &data)
if err != nil {
message = marshalError
} else if len(data.Errors) > 0 {
message = data.Errors[0]
}
}
return &httpClientError{ErrorMsg: message}
}
func NewServerError(response *http.Response) error {
var serverError *httpClientError
body, _ := buildLimitedRawResponse(response)
err := NewAPIClientError(body)
if errors.As(err, &serverError) && len(body) > 0 {
serverError.Response = bytes.NewBuffer(body)
return serverError
}
return err
}