Skip to content
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

Closed
abskrj opened this issue Dec 27, 2022 · 5 comments
Closed

Write error response schema for APIs #13

abskrj opened this issue Dec 27, 2022 · 5 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@abskrj
Copy link
Member

abskrj commented Dec 27, 2022

We need to write an Error response schema to be returned from API. Like this

{
    "error": "Error parsing JSON request",
    "message": "Invalid JSON format",
    "detail": "Post the 'author' and 'id' fields in the request body."
    "statusCode": 400
}
@scoobhidu
Copy link

I would like to pick this up @abhishekraj272

@abskrj
Copy link
Member Author

abskrj commented Dec 29, 2022

ignore the above message ^^

@abskrj abskrj assigned abskrj and scoobhidu and unassigned abskrj Dec 29, 2022
@scoobhidu
Copy link

scoobhidu commented Dec 31, 2022

@abhishekraj272 shouldn't the status code be set in the API response only instead of sending in body?

@uzaxirr
Copy link
Member

uzaxirr commented Jan 7, 2023

A map should be added in constants/constants.go

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 types/types.go this file will contain the struct which can be used for both Error and Sucess Response

types/types.go

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 /generate-presigned-url

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 Data feild will be empty while the Error field will have the appropriate info

@uzaxirr uzaxirr added good first issue Good for newcomers help wanted Extra attention is needed labels Jan 17, 2023
@uzaxirr uzaxirr pinned this issue Jan 17, 2023
@abskrj abskrj closed this as completed Nov 6, 2023
@abskrj abskrj unpinned this issue Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants