error check: refactor code to remove warning
This commit is contained in:
@@ -41,15 +41,18 @@ func ValidateId(db *gorm.DB, id uint, value any) error {
|
||||
}
|
||||
|
||||
func ReturnErrorsAsJsonResponse(ac *appcontext.AppContext, err error) {
|
||||
var httpError HttpError
|
||||
var ve validator.ValidationErrors
|
||||
if errors.As(err, &ve) {
|
||||
|
||||
ve, isValidationErrors := errors.AsType[validator.ValidationErrors](err)
|
||||
if isValidationErrors {
|
||||
ac.C.JSON(http.StatusBadRequest, getValidationErrors(ac, &ve))
|
||||
} else if errors.As(err, &httpError) {
|
||||
ac.C.JSON(httpError.StatusCode, gin.H{"error": httpError.Err.Error()})
|
||||
} else {
|
||||
ac.C.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
httpError, isHttpError := errors.AsType[HttpError](err)
|
||||
if isHttpError {
|
||||
ac.C.JSON(httpError.StatusCode, gin.H{"error": httpError.Err.Error()})
|
||||
return
|
||||
}
|
||||
ac.C.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
}
|
||||
|
||||
func (h HttpError) Error() string {
|
||||
|
||||
Reference in New Issue
Block a user