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) {
|
func ReturnErrorsAsJsonResponse(ac *appcontext.AppContext, err error) {
|
||||||
var httpError HttpError
|
|
||||||
var ve validator.ValidationErrors
|
ve, isValidationErrors := errors.AsType[validator.ValidationErrors](err)
|
||||||
if errors.As(err, &ve) {
|
if isValidationErrors {
|
||||||
ac.C.JSON(http.StatusBadRequest, getValidationErrors(ac, &ve))
|
ac.C.JSON(http.StatusBadRequest, getValidationErrors(ac, &ve))
|
||||||
} else if errors.As(err, &httpError) {
|
return
|
||||||
ac.C.JSON(httpError.StatusCode, gin.H{"error": httpError.Err.Error()})
|
|
||||||
} else {
|
|
||||||
ac.C.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
||||||
}
|
}
|
||||||
|
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 {
|
func (h HttpError) Error() string {
|
||||||
|
|||||||
Reference in New Issue
Block a user