Skip to content

Usage Issue: Custom Error Types #3686

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

Open
mster429 opened this issue Mar 30, 2025 · 1 comment
Open

Usage Issue: Custom Error Types #3686

mster429 opened this issue Mar 30, 2025 · 1 comment

Comments

@mster429
Copy link

In my design file I define this custom error response

var CommonErrorResponse = Type("CommonErrorResponse", func() {
	Description("Common error response for errors")
	Field(1, "name", String, "Error name", func() {
		Meta("struct:error:name") // Tells Goa this field contains the error name
	})
	Field(2, "status", Int, "Response status", func() {
		Enum(0, 1)
	})
	Field(3, "message", String, "Response message")

	Field(4, "data", Any, "Response data")
	Field(5, "timestamp", String, "Response timestamp", func() {
		Format(FormatDateTime)
	})

	Required("name", "status", "message", "timestamp")
})

And i use it in the service like this

var _ = API("app", func() {
	Title("Payment API")
	Description("Payment API for QR and IPG payments")
	Server("app", func() {
		Services("app")
	})
})

var _ = Service("app", func() {
	Description("Payment Service")
	Files("/openapi.json", "gen/http/openapi3.json")

	Method("createIPGOrder", func() {
		Description("Create an order for IPG payment")
		Security(APIKeyAuth)
		Payload(CreateIPGPaymentOrderRequest)
		Result(HTTPCommonResponse)
		Error("bad_request", CommonErrorResponse, "Bad Request")
		Error("internal_error", CommonErrorResponse, "Internal Server Error")
		Error("unauthorized", CommonErrorResponse, "Operation failed due to unauthorized access")
		Error("not_found", CommonErrorResponse, "Resources not found")
		Error("conflicts", CommonErrorResponse, "Existing Order")

		HTTP(func() {
			POST("xxxxxx")
			Header("xxxxxxxx")
			Response(StatusCreated)
			Response("bad_request", StatusBadRequest)
			Response("not_found", StatusNotFound)
			Response("internal_error", StatusInternalServerError)
			Response("unauthorized", StatusUnauthorized)
			Response("conflicts", StatusConflict)
		})
	})
}

and in my service implementation i implement it like this


		return nil, &app.CommonErrorResponse{
			Name:      "internal_error",
			Status:    1,
			Message:   "MerchantId is not parsed from auth middleware or not a string",
			Timestamp: time.Now().Format(time.RFC3339),
		}

But the response i get is still the default ServiceError by goa. Is this the expected behavior or is there something I do wrong ?

Response I get

409 Conflict
{
    "name": "fault",
    "id": "b8E_z1DW",
    "message": "Common error response for errors",
    "temporary": false,
    "timeout": false,
    "fault": true
}

What I expect

409 Conflict
{
    "status": 1,
    "message": "MerchantId is not parsed from auth middleware or not a string",
    "timestamp": "2025-03-14T18:09:27+05:30"
}
@raphael
Copy link
Member

raphael commented Apr 1, 2025

I don't see anything obviously wrong with the above (other than the name of the returned error would have to be "conflicts" to return 409 and not "internal_error" but I'm guessing that's just a copy/paste error :) ). Would you be able to provide a small complete repro? Also FYI Meta("struct:error:name") has been deprecated in favor of ErrorName (but it should still work).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants