emersion / go-webdav

A Go library for WebDAV, CalDAV and CardDAV
MIT License
326 stars 72 forks source link

Add error type representing DAV/XML errors #56

Closed bitfehler closed 2 years ago

bitfehler commented 2 years ago

Backends will need some way to signal that a precondition error occurred (and specifying which one) without causing the server to return a 500. This commit adds an exported function to create a specific error for this. The existing error handling routine is slightly adapted to handle this error in such a way that it (almost) returns the desired result.

Usage would be something like:

return "", carddav.PreconditionError("no-uid-conflict")

As is, this approach is missing a few things to be correct:

I'd like to get your opinion as early as possible before I start writing more code.

Other approaches I could think of would be creating another, more specific error type for this, using the context for communicating specifics, or adding a third return value to the relevant interface funcions.

What I think is nice about this approach is that it leaves the interfaces as is and does not require handling of this kind of errors in all the places they could occur.

What do you think?

bitfehler commented 2 years ago

Of course this would not just be for backends. I think it would help take care of all these TODOs: https://github.com/emersion/go-webdav/blob/master/carddav/server.go#L406-L421

emersion commented 2 years ago

I like the idea of using a special error type for this. Although maybe it'd be better to have a new internal.DAVError type, and special-case it in internal.ServeError to do the proper XML marshalling and Content-Type etc.

bitfehler commented 2 years ago

Totally agree, did this now. Also moved some stuff around, let me know if you'd like to see something elsewhere. I also update the commit message with some details. In general I think this will be a useful mechanism. Of course something might show up down the road that might require some modification, but I guess that's life...

bitfehler commented 2 years ago

I think I addressed all your comments