Spaces:
Configuration error
Configuration error
File size: 3,717 Bytes
2bf583e c2003d2 decc167 c2003d2 2bf583e 212dffe 2bf583e 0199ba6 decc167 2bf583e c2003d2 2bf583e c2003d2 2bf583e c2003d2 2bf583e c2003d2 decc167 c2003d2 2bf583e 602f6ba | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | 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)
}
|