File size: 4,411 Bytes
48471f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
143
144
145
146
147
148
149
150
151
152
153
package response

import (
	errors "api.qobiltu.id/apperror"
	"api.qobiltu.id/models"
	goErrors "errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"net/http"
)

type ErrorResponse struct {
	Details          string            `json:"details"`
	ValidationErrors []ValidationError `json:"validation_errors,omitempty"`
	models.Exception
}

type API struct {
	Status   string         `json:"status"`
	Message  string         `json:"message,omitempty"`
	Data     any            `json:"data,omitempty"`
	MetaData any            `json:"meta_data"`
	Error    *ErrorResponse `json:"errors,omitempty"`
}

type List struct {
	List any `json:"list"`
}

type ValidationError struct {
	Field   string `json:"field"`
	Message string `json:"message"`
}

func HandleError(c *gin.Context, err error) {

	apiResponse := API{
		Status:  "error",
		Message: "An error occurred, " + err.Error(),
		Error: &ErrorResponse{
			Exception: models.Exception{Message: ""},
		},
		MetaData: struct{}{},
	}

	var appErr *errors.AppError

	if goErrors.As(err, &appErr) {
		apiResponse.Error.Details = fmt.Sprintf("%v", appErr.Err)

		switch specificErr := appErr.Err.(type) {
		case errors.ValidationError:
			validationError := make([]ValidationError, len(specificErr.Errors))
			apiResponse.Message = "Validation Error"
			apiResponse.Error.ValidationErrors = validationError
			for i, ve := range specificErr.Errors {
				apiResponse.Error.ValidationErrors[i] = ValidationError{
					Field:   ve.Field,
					Message: ve.Message,
				}
			}
			c.JSON(http.StatusBadRequest, apiResponse)
			return
		case errors.ConflictError:
			apiResponse.Message = specificErr.Message
			apiResponse.Error.Details = specificErr.Error()
			apiResponse.Error.Exception.DataDuplicate = true
			c.JSON(http.StatusConflict, apiResponse)
			return
		case errors.InternalError:
			apiResponse.Message = specificErr.Message
			apiResponse.Error.Details = specificErr.Error()
			apiResponse.Error.Exception.InternalServerError = true
			c.JSON(http.StatusInternalServerError, apiResponse)
			return
		case errors.NotFoundError:
			apiResponse.Message = specificErr.Message
			apiResponse.Error.Details = specificErr.Error()
			apiResponse.Error.Exception.DataNotFound = true
			c.JSON(http.StatusNotFound, apiResponse)
			return
		case errors.UnauthorizedError:
			apiResponse.Message = specificErr.Message
			apiResponse.Error.Details = specificErr.Error()
			apiResponse.Error.Exception.Unauthorized = true
			c.JSON(http.StatusUnauthorized, apiResponse)
			return
		case errors.ForbiddenError:
			apiResponse.Message = specificErr.Message
			apiResponse.Error.Details = specificErr.Error()
			apiResponse.Error.Exception.Forbidden = true
			c.JSON(http.StatusForbidden, apiResponse)
			return
		case errors.BadRequestError:
			apiResponse.Message = specificErr.Message
			apiResponse.Error.Details = specificErr.Error()
			apiResponse.Error.Exception.BadRequest = true
			c.JSON(http.StatusBadRequest, apiResponse)
			return
		default:
			apiResponse.Error.Details = "An unexpected error occurred."
			apiResponse.Error.Exception.InternalServerError = true
			c.JSON(http.StatusInternalServerError, apiResponse)
			return
		}
	} else if validationErrors, ok := err.(validator.ValidationErrors); ok {
		apiResponse.Error.Details = "Validation failed for the request."
		apiResponse.Error.Exception.ValidationError = true
		apiResponse.Error.ValidationErrors = make([]ValidationError, len(validationErrors))
		for i, fe := range validationErrors {
			apiResponse.Error.ValidationErrors[i] = ValidationError{
				Field:   fe.Field(),
				Message: fe.Translate(nil), // adjust if you use translator
			}
		}
		c.JSON(http.StatusBadRequest, apiResponse)
		return
	}

	c.JSON(http.StatusInternalServerError, apiResponse)
	apiResponse.Error.Details = err.Error()
}

func HandleSuccess(c *gin.Context, message string, statusCode int, data any, metaData any) {
	apiResponse := API{
		Status:   "success",
		Message:  message,
		Data:     data,
		MetaData: metaData,
	}

	if metaData == nil {
		apiResponse.MetaData = struct{}{}
	}

	c.JSON(statusCode, apiResponse)
}

func HandleSuccessWithPaging(c *gin.Context, message string, content any, pagingInfo *PagingInfo) {

	apiResponse := API{
		Message: message,
		Status:  "success",
		Data: List{
			List: content,
		},
		MetaData: pagingInfo,
	}

	c.JSON(http.StatusOK, apiResponse)
}