@@ -31,106 +31,6 @@ import (
31
31
// use of writeJsonRespStructured is preferable. it api exists due to historical reason
32
32
// err.message is used as internal message for ApiError object in resp
33
33
func WriteJsonResp (w http.ResponseWriter , err error , respBody interface {}, status int ) {
34
- response := Response {}
35
- if err == nil {
36
- response .Result = respBody
37
- } else if apiErr , ok := err .(* util.ApiError ); ok {
38
- response .Errors = []* util.ApiError {apiErr }
39
- if apiErr .HttpStatusCode != 0 {
40
- status = apiErr .HttpStatusCode
41
- }
42
- } else if validationErrs , ok := err .(validator.ValidationErrors ); ok {
43
- var valErrors []* util.ApiError
44
- for _ , validationErr := range validationErrs {
45
- //validationErr
46
- valErr := & util.ApiError {
47
- UserMessage : fmt .Sprint (validationErr ),
48
- InternalMessage : fmt .Sprint (validationErr ),
49
- }
50
- valErrors = append (valErrors , valErr )
51
- }
52
- response .Errors = valErrors
53
- } else if util .IsErrNoRows (err ) {
54
- status = http .StatusNotFound
55
- // Try to extract resource context from respBody for better error messages
56
- resourceType , resourceId := extractResourceContext (respBody )
57
- if resourceType != "" && resourceId != "" {
58
- // Create context-aware resource not found error
59
- apiErr := util .NewResourceNotFoundError (resourceType , resourceId )
60
- response .Errors = []* util.ApiError {apiErr }
61
- } else {
62
- // Fallback to generic not found error (no more "pg: no rows in result set")
63
- apiErr := util .NewGenericResourceNotFoundError ()
64
- response .Errors = []* util.ApiError {apiErr }
65
- }
66
- } else if multiErr , ok := err .(* multierror.Error ); ok {
67
- var errorsResp []* util.ApiError
68
- for _ , e := range multiErr .Errors {
69
- errorResp := & util.ApiError {
70
- UserMessage : e .Error (),
71
- InternalMessage : e .Error (),
72
- }
73
- errorsResp = append (errorsResp , errorResp )
74
- }
75
- response .Errors = errorsResp
76
- } else if errStatus , ok := err .(* errors2.StatusError ); ok {
77
- apiErr := & util.ApiError {}
78
- apiErr .Code = strconv .Itoa (int (errStatus .ErrStatus .Code ))
79
- apiErr .InternalMessage = errStatus .Error ()
80
- apiErr .UserMessage = errStatus .Error ()
81
- response .Errors = []* util.ApiError {apiErr }
82
- } else if unexpectedObjectError , ok := err .(* errors2.UnexpectedObjectError ); ok {
83
- apiErr := & util.ApiError {}
84
- apiErr .InternalMessage = unexpectedObjectError .Error ()
85
- apiErr .UserMessage = unexpectedObjectError .Error ()
86
- response .Errors = []* util.ApiError {apiErr }
87
- } else {
88
- apiErr := & util.ApiError {}
89
- apiErr .Code = "000" // 000=unknown
90
- apiErr .InternalMessage = errors .Details (err )
91
- if respBody != nil {
92
- apiErr .UserMessage = respBody
93
- } else {
94
- apiErr .UserMessage = err .Error ()
95
- }
96
- response .Errors = []* util.ApiError {apiErr }
97
- }
98
- response .Code = status //TODO : discuss with prashant about http status header
99
- response .Status = http .StatusText (status )
100
-
101
- b , err := json .Marshal (response )
102
- if err != nil {
103
- util .GetLogger ().Errorw ("error in marshaling err object" , "err" , err )
104
- status = 500
105
-
106
- response := Response {}
107
- apiErr := & util.ApiError {}
108
- apiErr .Code = "0000" // 000=unknown
109
- apiErr .InternalMessage = errors .Details (err )
110
- apiErr .UserMessage = "response marshaling error"
111
- response .Errors = []* util.ApiError {apiErr }
112
- b , err = json .Marshal (response )
113
- if err != nil {
114
- b = []byte ("response marshaling error" )
115
- util .GetLogger ().Errorw ("Unexpected error in apiError" , "err" , err )
116
- }
117
- }
118
- if status > 299 || err != nil {
119
- util .GetLogger ().Infow ("ERROR RES" , "TYPE" , "API-ERROR" , "RES" , response .Code , "err" , err )
120
- }
121
- w .Header ().Set (CONTENT_TYPE , APPLICATION_JSON )
122
- w .WriteHeader (status )
123
- _ , err = w .Write (b )
124
- if err != nil {
125
- util .GetLogger ().Error (err )
126
- }
127
-
128
- }
129
-
130
- // This version will have more cases handled for like for 409(till now)
131
- // can be used in multiple places for suitable status code in response
132
- // Made a new version for backwards compatibility
133
- func WriteJsonRespV2 (w http.ResponseWriter , err error , respBody interface {}, status int ) {
134
34
response := Response {}
135
35
if err == nil {
136
36
response .Result = respBody
0 commit comments