-
-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Write error response schema for APIs #13
Comments
I would like to pick this up @abhishekraj272 |
ignore the above message ^^ |
@abhishekraj272 shouldn't the status code be set in the API response only instead of sending in body? |
A map should be added in const HTTPStatusCodes := map[int]string{
http.StatusOK: http.StatusText(http.StatusOK),
http.StatusCreated: http.StatusText(http.StatusCreated),
http.StatusAccepted: http.StatusText(http.StatusAccepted),
http.StatusNonAuthoritativeInfo: http.StatusText(http.StatusNonAuthoritativeInfo),
http.StatusNoContent: http.StatusText(http.StatusNoContent),
http.StatusBadRequest: http.StatusText(http.StatusBadRequest),
http.StatusUnauthorized: http.StatusText(http.StatusUnauthorized),
http.StatusForbidden: http.StatusText(http.StatusForbidden),
http.StatusNotFound: http.StatusText(http.StatusNotFound),
http.StatusMethodNotAllowed: http.StatusText(http.StatusMethodNotAllowed),
http.StatusConflict: http.StatusText(http.StatusConflict),
http.StatusInternalServerError: http.StatusText(http.StatusInternalServerError),
} Create a new directory and file
type Response struct {
Status int `json:"status"`
Data interface{} `json:"data"`
Error interface{} `json:"error"`
} and then you can modify the controller, an example of what the Ping controller may look like is func Ping(c *gin.Context) {
response := Response{
Status: HTTPStatusCodes[http.StatusOK],
Data: "pong",
Error: struct{}{},
}
c.JSON(http.StatusOK, response)
} It may give the below output {
"status": 200,
"data": "pong",
"error": {}
} And for the response := Response{
Status: HTTPStatusCodes[http.StatusOK],
Data: map[string]interface{}{
"preSignedURL": "https://somebucket-bucket.s3.amazonaws.com/abcdefg",
"videoID": "109934677.mp4",
},
Error: struct{}{},
} and in case of error the |
We need to write an Error response schema to be returned from API. Like this
The text was updated successfully, but these errors were encountered: