Skip to content
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

Use APIError #132

Closed
agorman opened this issue Jan 9, 2020 · 9 comments · Fixed by #146
Closed

Use APIError #132

agorman opened this issue Jan 9, 2020 · 9 comments · Fixed by #146

Comments

@agorman
Copy link

agorman commented Jan 9, 2020

Is your feature request related to a problem? Please describe.
It would be nice to get the status code returned from Keycloak.

Describe the solution you'd like
Return APIErrors from gocloak calls. This could be tricky for errors resulting from things other than Keycloak API calls.

Describe alternatives you've considered
Can't think of any though I'm totally open to any existing solutions I'm unaware of.

Additional context
I have my own HTTP service in front of gocloak and right now it's hard to determine what errors and response codes I should return. For example I might get an error back like this "400 Bad Request: Password policy not met. See logs for details".

I'd like to be able to set my response code to 400 and set an appropriate error of "Password policy not met." The "See logs for details" part doesn't really make sense but I'm not sure there is anything that can be done to solve that.

@SVilgelm
Copy link
Collaborator

SVilgelm commented Feb 9, 2020

Do you mean anything special by APIError? Any libraries or interfaces?

@Nerzal
Copy link
Owner

Nerzal commented Feb 9, 2020

I could imagine using something like this when returning errors

// APIError holds message and statusCode for api errors
type APIError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Detail  string `json:"detail,omitempty"`
}

// Error stringifies the APIError
func (apiError APIError) Error() string {
	return "Code: " + strconv.Itoa(apiError.Code) + "Message: " + apiError.Message + "Detail: " + apiError.Detail
}

@Nerzal
Copy link
Owner

Nerzal commented Feb 9, 2020

When improving the error handling, we could also use error wrapping. In order to provide better errors, that are way more helpful when searching for problems.

My favorite package for error wrapping is:
https://github.com/pkg/errors

I stole the error wrapping stuff from dave cheney and i'm using it everywhere. Makes life easier.
https://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully

@agorman
Copy link
Author

agorman commented Feb 9, 2020

I was actually just referring to the APIError struct that already exists. Though I like Nerzal's improvement above.

https://github.com/Nerzal/gocloak/blob/master/models.go#L58

Error wrapping is also now standard library as of go 1.13. Not sure if you want to make that a requirement though.

https://blog.golang.org/go1.13-errors

@Nerzal
Copy link
Owner

Nerzal commented Feb 10, 2020

@SVilgelm what do you think?
We could just pack the information inside an APIError object inside the checkForErrors method and return this as error.

Optional (Additionaly):
We could wrap the errors to provide even better error handling

@SVilgelm
Copy link
Collaborator

wrapping errors means adding additional restrictions of go 1.13, am I right?
I would prefer to use wrapping, due to better handling

@Nerzal
Copy link
Owner

Nerzal commented Feb 10, 2020

we don't have to restrict to go 1.13 if we use the pkg/errors lib for that.
It is the better implementation imho, also dave chaney contributed to it (must be good) :D

@SVilgelm
Copy link
Collaborator

Ok, let's do it :)

@Nerzal
Copy link
Owner

Nerzal commented Feb 10, 2020

i'll do that. shouldn't take long

@SVilgelm SVilgelm linked a pull request Feb 20, 2020 that will close this issue
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

Successfully merging a pull request may close this issue.

3 participants