mirror of
https://github.com/hanzozt/zt.git
synced 2026-10-30 15:04:55 +00:00
- Repo names: ziti-* → zt-* - Go modules: github.com/hanzozt/ziti → github.com/hanzozt/zt - Docker images: hanzozt/ziti-* → hanzozt/zt-* - C/Java/JS symbols: ziti_* → zt_* - File and directory names updated
39 lines
770 B
Go
39 lines
770 B
Go
package models
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/hanzozt/foundation/v2/errorz"
|
|
"github.com/hanzozt/storage/boltz"
|
|
"github.com/hanzozt/zt/v2/controller/apierror"
|
|
)
|
|
|
|
func ToApiError(err error) *errorz.ApiError {
|
|
return ToApiErrorWithDefault(err, errorz.NewUnhandled)
|
|
}
|
|
|
|
func ToApiErrorWithDefault(err error, f func(err error) *errorz.ApiError) *errorz.ApiError {
|
|
var apiErr *errorz.ApiError
|
|
if errors.As(err, &apiErr) {
|
|
return apiErr
|
|
}
|
|
|
|
if boltz.IsErrNotFoundErr(err) {
|
|
result := errorz.NewNotFound()
|
|
result.Cause = err
|
|
return result
|
|
}
|
|
|
|
var fe *errorz.FieldError
|
|
if errors.As(err, &fe) {
|
|
return errorz.NewFieldApiError(fe)
|
|
}
|
|
|
|
var sve *apierror.ValidationErrors
|
|
if errors.As(err, &sve) {
|
|
return errorz.NewCouldNotValidate(sve)
|
|
}
|
|
|
|
return f(err)
|
|
}
|