November 28, 2022

Mapping .NET exceptions to XPath Errors

Because why not?

The namespace http://www.w3.org/2005/xqt-errors# (shortened to err:, also described here and here) hosts a number of error codes relevant in the context of XPath functions, XSLT or XQuery. While these errors are usually useful only to XML processors, there is really no reason not to use them when describing errors in general. Imagine you are trying to use RDF to describe the result of a process, or perhaps monitoring a single programming function. The function may fail, end in an exception or otherwise not produce a desired effect, in which case it is useful to be able to use a standardized identification for the cause of the error. Of course the original exception would be more useful to people actually fixing the error, but this should be "language-agnostic", potentially translatable to any different language which uses similar concept of exceptions or errors.

I decided to browse the error codes listed in the namespace, and tried to match them with exception types in .NET. There are not that many of them that align, but the useful ones are still representable. Coupled with properties like err:code, err:description, err:value and err:line-number, these may be useful when describing arbitrary program errors in RDF.

Let's start general...

Exception

This likely maps to err:FOER0000 ("Unidentified error."), but preferably only when the concrete exception type is actually unavailable (i.e. the type is exactly Exception, or perhaps SystemException or ApplicationException).

ArgumentException

Generally thrown when you pass a bad argument to a method; this may map to err:FORG0006 ("Invalid argument type."), if you take "type" to have a broad meaning here. In the case of ArgumentOutOfRangeException, err:FOJS0005 ("Invalid options.") might also be approriate.

IndexOutOfRangeException

A common exception if you don't check the index of a collection. This may map to err:FOAY0001 ("Array index out of bounds."). I think this can easily apply even if the indexed collection is not an array.

DivideByZeroException

One of the oldest errors in general; this maps easily to err:FOAR0001 ("Division by zero.").

OverflowException

A similar error, this time representable as err:FOAR0002 ("Numeric operation overflow/underflow."). There are also more specific codes: err:FODT0001 ("Overflow/underflow in date/time operation.") and err:FODT0002 ("Overflow/underflow in duration operation."), but those errors are not distinguished in .NET.

NotFiniteNumberException

This kind of exception is generally not encountered much, but you might see it if you work in contexts where infinite floating-point numbers or NaNs are not allowed. If the exception was caused by an infinity, err:FOAR0002 should be used again, otherwise for NaNs err:FOCA0005 ("NaN supplied as float/double value.") is the way to go.

InvalidCastException

A common exception if you forget to use generics; this may be represented as err:FORG0001 ("Invalid value for cast/constructor.").

FormatException

A similar exception, thrown when an attempt to parse a string in an invalid format is made. This can be trated as err:FOCA0002 ("Invalid lexical value.") which sounds differently, but basically means the same thing.

UriFormatException

Thrown if you attempt to construct a URI from an invalid string. This may be mapped to either err:FORG0001 or err:FOCA0002, but I think err:FORG0002 ("Invalid argument to fn:resolve-uri().") carries the meaning best.

InvalidTimeZoneException

Quite a specific exception, but still mapping to err:FODT0003 ("Invalid timezone value.") trivially.

NullReferenceException, IOException, UnauthorizedAccessException

All of these exceptions can be mapped to err:FODC0002 ("Error retrieving resource."), although (especially in the null reference case) by a long stretch.


There are also error codes which, while they are quite specific, seem to be able to match something in .NET, but I am unsure what exactly:

err:XPDY0002

"It is a dynamic error if evaluation of an expression relies on some part of the dynamic context that has not been assigned a value."

This sounds like something that could be raised from a dynamic language, like when you use an unassigned or non-existing global variable.

err:XPDY0130

"An implementation-dependent limit has been exceeded."

I mean there are a lot of exceptions that can be described in this way, like TimeoutException, OutOfMemoryException, StackOverflowException, but it's not specific enough in order to be useful.

err:XPTY0004 and err:XPST0017

"It is a type error if, during the static analysis phase, an expression is found to have a static type that is not appropriate for the context in which the expression occurs, or during the dynamic evaluation phase, the dynamic type of a value does not match a required type as specified by the matching rules."

"It is a static error if the expanded QName and number of arguments in a static function call do not match the name and arity of a function signature in the static context."

This sounds a lot like something that would come out of using dynamic, but in .NET, all these exceptions are generally thrown as RuntimeBinderException.

No comments:

Post a Comment