Skip to content

Request to allow modification of the default return handler for error responses via code gen (goctl) tool or .api specification #4989

@PistonShot1

Description

@PistonShot1

The feature request here is to add support for handling custom bad request and errors in a more standardised way and lesser boiler plate that most other framework provides for handling custom error codes and bad request.

Describe the solution you'd like

Here is a modified version of the handler rather than directly written with httpx.ErrorCtx it can have custom writer that sends in JSON format , which is more standardised way of sending error message and allow handling custom error codes in an easier way.

// part of handler
.
.
l := auth.NewValidateIdpSignInLogic(r.Context(), svcCtx)
		resp, err := l.ValidateIdpSignIn(&req)
		if err != nil {
			// httpx.ErrorCtx(r.Context(), w, err)
			// Use the custom error response function
			response.ErrorJSON(w, http.StatusBadRequest, err.Error(), nil)
			return
		} else {
			if resp.Code > 400 {
				response.ErrorJSON(w, resp.Code, resp.Message, resp.Data)
				return
			}
			httpx.OkJsonCtx(r.Context(), w, resp)
			return
		}

Here is the underlying ErrorJSON function,

func ErrorJSON(w http.ResponseWriter, statusCode int, message string, data any) {
	errorResponse := types.Response{
		Code:    statusCode,
		Success: false,
		Message: strings.ReplaceAll(message, `"`, ""), //message,
		Data:    data,
	}
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(statusCode)
	json.NewEncoder(w).Encode(errorResponse)
}

So the feature request here is actually if it is possible to add feature that allows to modify this scope of the default generated handler via .api file specification or perhaps via goctl command flags ?

Metadata

Metadata

Assignees

Labels

area/goctlCategorizes issue or PR as related to goctl.featureMarks an issue or PR as related to a new feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions