Skip to content

how to get non-200 response content #2861

Open

Description

@minping-dream

Problem statement

when add swagger note like:

// swagger:route GET /users/{user_id} user getUser
//
//     Responses:
//       200: getUser
//       400: ErrorResponse
//       500: ErrorResponse
func (c *UserController) GetUser() {...}

and use swagger-go to generate client sdk code, then client use generated code to call the method like this:

responseApi, err := sdk.SdkClient.User.GetUser(req, nil)

this is ok when service response code is 200 and the responseApi is getUserOk struct。but when the response code is 400 or 500,how can i get the ErrorResponse?i sow when resp code is non-200, the ReadResponse method(called in Submit)only panic some msg instead of return the expected ErrorResponse

result, err := a.transport.Submit(op)
	if err != nil {
		return nil, err
	}
	success, ok := result.(*ModifyUserOK)
	if ok {
		return success, nil
	}
	// unexpected success response
	// safeguard: normally, absent a default response, unknown success responses return an error above: so this is a codegen issue
	msg := fmt.Sprintf("unexpected success response for modifyUser: API contract not enforced by server. Client expected to get an error, but got: %T", result)
	panic(msg)

so how can i get expected ErrorResponse ?

Swagger specification

latest, 0.30

Steps to reproduce

Environment

swagger version: x.x.x
go version: 1.17.4
OS: centos 7.2 x86_64

Activity

  1. casualjim commented on Nov 19, 2022

    @casualjim
    Member

    you can typeswitch on the error

    switch errResponse := err.(type) {
        case *NotFoundResponse:
           fmt.Println(errResponse.Message)
          ....
     }
  2. minping-dream commented on Nov 21, 2022

    @minping-dream
    Author

    thanks for your reply, however when i did so:

    responseApi, err := sdk.SdkClient.User.GetUser(req, nil)
    	if err != nil {
    		switch errResponse := err.(type) {
    		case *sdkModels.ErrorResponse:
                        ...
    		}
    
    		
    	}
    

    it show errors as:

    err (variable of type error) cannot have dynamic type *models.ErrorResponse (missing method Error)
    

    how can i fix it?
    ###############
    the ErrorResponse swagger defined as:

    // unit err response for non 200
    // swagger:response ErrorResponse
    type ErrorResponse struct {
    	//In: body
    	Body struct {
    		Err controllers.ErrorResponse `json:"error"`
    	}
    }
    

    and controllers.ErrorResponse defined as:

    type ErrorResponse struct {
    	Code    int    `json:"code"`
    	Message string `json:"message"`
    	Status  string `json:"status"`
    }
    

    what ErrorResponse in generated code like this:

    // ErrorResponse error response
    //
    // swagger:model ErrorResponse
    type ErrorResponse struct {
    
    	// code
    	Code int64 `json:"code,omitempty"`
    
    	// message
    	Message string `json:"message,omitempty"`
    
    	// status
    	Status string `json:"status,omitempty"`
    }
    

    the ErrorResponse type has Validate,ContextValidate,MarshalBinary,UnmarshalBinary but missing Error() method.
    even if i add Error() method on controllers.ErrorResponse,the Error() method can't show in generated swagger.yml,of course the sdkModels.ErrorResponse type missing Error() method in the generated sdk code. then the error pasted in first occures. so how can i fix it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions