mongodb / go-client-mongodb-atlas

Go Client for MongoDB Atlas
https://www.mongodb.com/docs/atlas/reference/api-resources-spec/
Apache License 2.0
79 stars 34 forks source link

fix: enable casting API errors into error object #459

Closed wtrocki closed 1 year ago

wtrocki commented 1 year ago

Description

Extending SDK with another useful helper method that will map API error objects into generic error that CLI, Terraform might use etc.

wtrocki commented 1 year ago

can APIError implement Error so it fullfils the error interface?, ideally, in backward compatible way?

Very good question. I thought of having error object defined separately (not as model) As for now API error is tied to the OpenAPI thus it is just an ordinary model. However our contracts for errors are very weak. I'm thinking what would be the best way forward in this case.

wtrocki commented 1 year ago

I have the solution! IMHO we can move all error handling methods to atlas_client.go - thus we would not need to have different imports and can extend existing structures (subject to volatility if we were to change error object name in future)

wtrocki commented 1 year ago

So generally based on our architecture Error object cannot be error. It needs to be API object only and it is always wrapped by GenericError that has more metadata and it is error object itself.

I have made GenericError object strongly typed now and rely 100% on Error object.

Both formats work out of the box right now:

err.Error() - used in CLI to print message prints backwards compatible message (with some extended content) v2.IsError and v2.IsErrorCode() give us error structures and way to handle specific errors.

With both in place we should have complete error suite. Good news is that this change doesn't require any adjustment in Atlas CLI and it will just work.