✨ Add options, mostly transformers
This commit is contained in:
parent
b1e4c28dfe
commit
b4b4f041d7
4 changed files with 121 additions and 11 deletions
41
options.go
Normal file
41
options.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue