Not all errors returned from management implements management.Error interface
nicolaigj opened this issue · comments
Description
I have an integration test to check my error handling when passing a faux ClientSecret to management.New() and made a subsequent call to Users.List() which I expected to fail. It did, but I also expected the error to implement the management.Error interface so I could pull out the status code, it does not, so my test panicked instead.
Package Version
v5.19.2
Affected Resources
I've only tested one endpoint, but I assume it applies to all resources.
management.Client.List
Sample Code
package main
import (
"log"
"gopkg.in/auth0.v5/management"
)
func main() {
m, err := management.New("auth0test.eu.auth0.com", management.WithClientCredentials("fauxID", "fauxSecret"))
if err != nil {
log.Fatal("error creating client:", err)
}
_, err = m.Client.List()
if err != nil {
log.Print(err)
log.Fatalf("error calling API with status code %d", err.(management.Error).Status())
}
}
Expected Behavior
I expected a log message containing the status code of the returned error.
Actual Behavior
Panic because the error does not implement the management.Error interface
Output
2021/10/15 08:32:09 request failed: Get "https://auth0test.eu.auth0.com/api/v2/clients?include_totals=true&per_page=50": oauth2: cannot fetch token: 401 Unauthorized
Response: {"error":"access_denied","error_description":"Unauthorized"}
panic: interface conversion: *fmt.wrapError is not management.Error: missing method Status
goroutine 1 [running]:
main.main()
/home/nicolaigj/Code/Gitlab/auth0-test/main.go:19 +0x11e
exit status 2
References
Community Note
- Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
- Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
- If you are interested in working on this issue or have submitted a pull request, please leave a comment
The simple solution is to check if the error satisfies the interface before calling Status(), but at least update the docs on the management.Error() interface, because now it says Error is an interface describing any error which could be returned by the Auth0 Management API.
Hi @nicolaigj,
It is not the intention to always return a management.Error
but in cases where the Auth0 Management API responds with a payload similar to the following:
{
"statusCode": 400,
"error": "Bad Request",
"message": "Payload validation error: ... ",
"errorCode": "invalid_body"
}
The error you are receiving is likely related to oauth2
which is why it doesn't conform to the interface.
You should indeed check if the error satisfies the interface before using it as such.