Files

45 lines
1.5 KiB
Go

package v3
import (
"net/http"
"github.com/hanzo-fi/go-libs/v5/pkg/transport/api"
"github.com/hanzo-fi/payments/internal/api/common"
"github.com/hanzo-fi/payments/internal/api/services"
"github.com/hanzo-fi/payments/internal/connectors"
"github.com/hanzo-fi/payments/internal/connectors/engine"
"github.com/hanzo-fi/payments/internal/storage"
"github.com/pkg/errors"
)
const (
ErrValidation = "VALIDATION"
ErrInvalidID = "INVALID_ID"
ErrMissingOrInvalidBody = "MISSING_OR_INVALID_BODY"
ErrUniqueReference = "CONFLICT"
ErrConnectorCapabilityNotSupported = "CONNECTOR_CAPABILITY_NOT_SUPPORTED"
)
func handleServiceErrors(w http.ResponseWriter, r *http.Request, err error) {
var capabilityNotSupported *engine.ErrConnectorCapabilityNotSupported
switch {
case errors.Is(err, storage.ErrDuplicateKeyValue):
api.BadRequest(w, ErrUniqueReference, err)
case errors.Is(err, storage.ErrNotFound):
api.NotFound(w, err)
case errors.Is(err, storage.ErrForeignKeyViolation):
api.BadRequest(w, ErrValidation, errors.Cause(err))
case errors.Is(err, storage.ErrValidation):
api.BadRequest(w, ErrValidation, err)
case errors.Is(err, services.ErrValidation), errors.Is(err, connectors.ErrValidation):
api.BadRequest(w, ErrValidation, err)
case errors.Is(err, services.ErrNotFound):
api.NotFound(w, err)
case errors.As(err, &capabilityNotSupported):
api.BadRequest(w, ErrConnectorCapabilityNotSupported, err)
default:
common.InternalServerError(w, r, err)
}
}