41 lines
756 B
Go
41 lines
756 B
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
|
|
rErrors "codeberg.org/danjones000/responsable-errors"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type config struct {
|
|
transformers []Transformer
|
|
}
|
|
|
|
type Transformer func(error) rErrors.ResponsableError
|
|
|
|
type Option func(config) config
|
|
|
|
func WithTransformer(tr Transformer) Option {
|
|
return func(c config) config {
|
|
c.transformers = append(c.transformers, tr)
|
|
return c
|
|
}
|
|
}
|
|
|
|
func WithDefaultTransformer() Option {
|
|
return WithTransformer(ginTransformer)
|
|
}
|
|
|
|
func ginTransformer(er error) rErrors.ResponsableError {
|
|
var err *gin.Error
|
|
if !errors.As(er, &err) {
|
|
return nil
|
|
}
|
|
|
|
switch err.Type {
|
|
case gin.ErrorTypePrivate:
|
|
return rErrors.NewInternalError("%w", err)
|
|
default:
|
|
return rErrors.NewBadRequest("%w", err)
|
|
}
|
|
}
|