Error raised inside rest:error annotated function
Hi All, I noticed that when handling an error in a rest:error annotated function, if inside this function another error is raised/occurs, this is treated as a “normal” unhandled error with the HTTP 500 code, also when another annotated function for the specific error exists.
Is my understanding of the behavior correct: errors handling with rest:error functions cannot be “chained"?
Thanks for a clarification.
Mauro
Hi …,
Your understanding is correct: error functions are not chained. If a function annotated with %rest:error raises an error itself, no further error function is looked up, and the error is returned as a regular server error (500). This is intentional and prevents endless loops, such as a handler that triggers its own error again.
If you need several stages of error handling, you can use try/catch inside the error function. Error functions are ordinary XQuery functions, so you can call another handler (or a shared helper function) directly from the catch clause. If you only want a specific status code and message, web:error($status, $message) is a handy option.
Hope this helps,Christian
Quoted text · Mauro Mugnaini · 9 lines
Mauro Mugnaini via BaseX-Talk <basex-talk at mailman.uni-konstanz.de> schrieb am Mittwoch, 14. Oktober 2026 um 17:52: > Hi All, > I noticed that when handling an error in a rest:error annotated function, if inside this function another error is raised/occurs, this is treated as a “normal” unhandled error with the HTTP 500 code, also when another annotated function for the specific error exists. > > Is my understanding of the behavior correct: errors handling with rest:error functions cannot be “chained"? > > Thanks for a clarification. > > Mauro
Written for BaseX 12.4. BaseX may have changed since: please check the current documentation.