Skip to content

Usage Issue: Custom Error Types #3686

Open
@mster429

Description

@mster429

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"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions