The tulip.xerror module, used to standardize error handling throughout the Tulip project. The Error instance created by most functions has a __tostring metatable function that renders a complete message with all extra fields printed.


Assuming local xerror = require 'tulip.xerror'.

#f = xerror.converter(ecode, ...)

Creates a converter function that converts any error into an Error instance with code set to ecode. Any additional argument is the name of the field where extra values in that position are set on the Error instance (corresponding to the values after the error message, which is always the second value after a falsy first value indicating an error).

#err = xerror.ctx(err, label[, attrs])

Adds context to the error. The label is a stack, so the last added label is the first printed in the error message, followed by the next, until the first that was added (labels are prepended to the error message like this: last-label: next-label: first-label: message).

The attrs argument, if provided, is a table where each key-value pair is added to the error object if and only if it did not exist yet. This is because if an attribute is already set, it is assumed that the call that set it (which was closer to where the error originated) knew more about that field, so it is not overwritten. It can be used to set new attributes and default values (e.g. it can set the error message if there wasn't any).

If err is not an Error instance, an Error is first created with err as the message.

#first, (err | msg, ...) = xerror.db(first, msg, ...)

Converter for DB calls that either converts the error to an ecode EDB or ESQL, depending if the error is in the connection to the DB or in the execution of SQL. Returns all values if first is truthy, or the falsy first value followed by the Error instance.

#first, (err | msg, ...) = xerror.inval(first, msg, ...)

Converter for field validation errors, where extra values are the field name and value, that set the ecode to EINVAL.

#first, (err | msg, ...) = xerror.io(first, msg, ...)

Converter for IO calls such as read/write to file and sockets that set the ecode to EIO and store the errno field.

#ok = xerror.is(err, ...)

Returns true if err is of any of the specified codes, false otherwise. Codes can be patterns.

#ok = xerror.is_errno(err, ...)

Returns true if err is an EIO error with the errno field set to any of the specified numbers, false otherwise.

#ok = xerror.is_sql_state(err, ...)

Returns true if err is an ESQL error with the sql_state field set to any of the specified codes, false otherwise. Codes can be patterns.

#v, err, ... = xerror.must(v, err, ...)

Raises an error if v is falsy, calling xerror.throw with the second value and any subsequent values to use in string.format of the message. Otherwise returns all values.

#xerror.throw(msg, ...)

Raises an error with msg, which can contain formatting verbs. Extra arguments are provided to string.format. If msg is not a string or is not stringable and no extra argument are provided, raises the msg value as-is.

Back to index

About this wiki

commit 3ebfbd288b8e5c95fdf8ce2027a0e94cfa1c8976
Author: Martin Angers <martin.n.angers@gmail.com>
Date:   2021-02-25T14:07:12-05:00

Update to reflect Request:validate_body
Clone this wiki
https://git.sr.ht/~mna/tulip-wiki (read-only)
git@git.sr.ht:~mna/tulip-wiki (read/write)