renabled / amzn-sp-api-go

Amazon Selling Partner API in Go
MIT License
16 stars 0 forks source link

Error casting issue #1

Closed nkev closed 1 year ago

nkev commented 1 year ago

Thanks for sharing a great always up-to-date API client!

The errors returned have custom error types, e.g. *catalog.SearchCatalogItemsBadRequest but it's not easy to print the error messages. It just prints &{Errors:[0x14000b0a4c0]} because the errors are pointers.

Is there a better way than going through every possible error type, like below?

    var printError = func(errs *models.ErrorList) {
        for i := 0; i < len(errs.Errors); i++ {
            fmt.Println(errs.Errors[i].Message)
        }
    }

    if err != nil {
        switch err.(type) {
        case *catalog.SearchCatalogItemsBadRequest:
            er := err.(*catalog.SearchCatalogItemsBadRequest)
            printError(er.Payload)
        case *catalog.SearchCatalogItemsForbidden:
            er := err.(*catalog.SearchCatalogItemsForbidden)
            printError(er.Payload)

        //case etc..
        default:
            fmt.Println(err.Error())
        }
        return
    }

Perhaps a helper function is needed in this library.

amrnt commented 1 year ago

Hi @nkev!

Thanks for reporting this. I understand the struggle here!

There is already a very old issue: https://github.com/go-swagger/go-swagger/issues/2590 but I think the idea you are suggesting by creating a helper function makes total sense.

The idea would be to extend the generated code with a new template, and I actually gave it a try: The output is not as reliable. I would rather to just stick with it as it is and add such helper methods on application level (as you already did).