whatwg / fs

File System Standard
https://fs.spec.whatwg.org/
Other
237 stars 20 forks source link

Specify error mappings #57

Open a-sully opened 2 years ago

a-sully commented 2 years ago

This came up in https://github.com/web-platform-tests/wpt/commit/b20e8343ce5ff1eada39dc1487b8b28ed903fff6#r85726061

Ideally API errors map uniquely to DOMExceptions, which make them actionable by the site. For example, if NoModificationAllowedError always indicates that a file is locked and NotAllowedError always indicates the site needs to requestPermission()on the handle, the site can respond differently to these errors.

Currently, developers have to figure out this mapping on their own, or read the whole spec and infer this mapping. It would be nice to include a table mapping API errors to DOMExceptions that developers can use as a reference. Is there precedent for having this type of table in a spec?

That being said, it could be tricky to add a mapping that truly guarantees an error corresponds to a given API error (especially outside of the OPFS, where there's a whole host of things that could happen on the underlying OS). At the very least, here's a list of the mappings I'm aware of as a starting point...

Some errors are mostly relevant outside of the OPFS:

annevk commented 2 years ago

"AbortError" is very specific and mainly for abort signals only. cc @domenic

I'd also recommend considering that implementations might want some flexibility going forward so requiring rather unique exceptions might not be worthwhile if web developers aren't expected to branch on the exception names anyway.

jesup commented 2 years ago

If there's specific action an application might want to take for a specific failure mode, then tightly specifying errors would be useful. Otherwise tightly specifying errors doesn't have much benefit I can see; though specifying them avoids unintentional lock-in by developers who look for a specific (non-specced) error, and then break if it changes -- or if they use a different browser. However, we should be in alignment with how other specs do (or should) specify errors. @annevk ? @domenic ?

We throw SecurityError for some cases that violate security rules.

Instead of AbortError for non-implemented features, maybe NotSupportedError?

domenic commented 2 years ago

So you definitely need to specify the error type, and all browsers need to agree. There's no option of not specifying the error type at all.

But you don't need to make sure every error type is used perfectly and maps to distinct cases, if developers don't need to distinguish between such cases. For example, you could just always throw a TypeError (the web platform's generic error). Or, you could just lump a bunch of error conditions together under "OperationError" DOMException, or "InvalidStateError" DOMException. That's a reasonable option.

Specific error type guidance you should be consistent with, which is unfortunately not written down or consistently applied, is:

Note that "TypeMismatchError" DOMException is deprecated.

So yeah, there are some improvements you could make here to align better with the rest of the platform.