package response import ( "encoding/json" "errors" "net/http" "strconv" "api.qobiltu.id/models" "api.qobiltu.id/pkg/validation" "api.qobiltu.id/utils" "github.com/gin-gonic/gin" ) func HandleError(c *gin.Context, err error) { var exception models.Exception if errors.As(err, &exception) { utils.LogError(exception.Err) switch { case exception.DataDuplicate: responseError(c, http.StatusConflict, exception) case exception.Unauthorized: responseError(c, http.StatusUnauthorized, exception) case exception.DataNotFound: responseError(c, http.StatusNotFound, exception) case exception.Forbidden: responseError(c, http.StatusForbidden, exception) case exception.BadRequest: responseError(c, http.StatusBadRequest, exception) case exception.InternalServerError: responseError(c, http.StatusInternalServerError, exception) case exception.QueryError: responseError(c, http.StatusInternalServerError, exception) case exception.InvalidPasswordLength: responseError(c, http.StatusBadRequest, exception) case exception.IsPassTheLimit: responseError(c, http.StatusTooManyRequests, exception) case exception.IsTimeOut: responseError(c, http.StatusRequestTimeout, exception) case exception.AttemptNotFound: responseError(c, http.StatusNotFound, exception) case exception.QuizTimeExpired: responseError(c, http.StatusBadRequest, exception) case exception.QuizAlreadyFinished: responseError(c, http.StatusBadRequest, exception) case exception.QuizAttemptLimit: responseError(c, http.StatusBadRequest, exception) case exception.AcademyNotFinished: responseError(c, http.StatusBadRequest, exception) case exception.QuizAlreadyPassed: responseError(c, http.StatusBadRequest, exception) case exception.ValidationError: responseValidationError(c, http.StatusUnprocessableEntity, exception.ValidationErrorFields) default: responseError(c, http.StatusInternalServerError, exception) } return } var ( jsonSyntaxError *json.SyntaxError jsonUnmarshalTypeError *json.UnmarshalTypeError ) if errors.As(err, &jsonSyntaxError) { responseError(c, http.StatusBadRequest, models.Exception{ BadRequest: true, Message: "Invalid JSON syntax", Err: err, }) return } if errors.As(err, &jsonUnmarshalTypeError) { responseError(c, http.StatusBadRequest, models.Exception{ BadRequest: true, Message: "Invalid body request", Err: err, }) return } var numErr *strconv.NumError if errors.As(err, &numErr) { responseError(c, http.StatusBadRequest, models.Exception{ BadRequest: true, Message: "Invalid number format", Err: err, }) return } utils.LogError(err) responseError(c, http.StatusInternalServerError, models.Exception{ InternalServerError: true, Message: "Internal Server Error", }) } func HandleSuccess(c *gin.Context, status int, msg string, data any, metaData any) { res := models.SuccessResponse{ Status: "success", Message: msg, Data: data, MetaData: metaData, } c.JSON(status, res) } func responseError(c *gin.Context, status int, exception models.Exception) { message := exception.Message exception.Message = "" res := models.ErrorResponse{ Status: "error", Message: message, Errors: exception, } c.AbortWithStatusJSON(status, res) } func responseValidationError(c *gin.Context, status int, validationErrors []validation.ErrorMessage) { res := models.ErrorResponse{ Status: "error", Message: "Validasi data gagal.", Errors: models.Exception{ ValidationError: true, ValidationErrorFields: validationErrors, }, } c.AbortWithStatusJSON(status, res) }