util/error.lua
author Matthew Wild <mwild1@gmail.com>
Sun, 17 Mar 2024 10:10:24 +0000
changeset 13464 a688947fab1e
parent 13083 e7a5e5a0dc02
permissions -rw-r--r--
mod_bosh: Set base_type on session This fixes a traceback with mod_saslauth. Ideally we move this to util.session at some point, though.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12979
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 11228
diff changeset
     1
local id = require "prosody.util.id";
11055
08539aa129ee util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents: 11054
diff changeset
     2
11228
8143fd2f138b util.error: Switch to util.debug traceback tables and remove display_tracebacks option
Matthew Wild <mwild1@gmail.com>
parents: 11227
diff changeset
     3
local util_debug; -- only imported on-demand
8143fd2f138b util.error: Switch to util.debug traceback tables and remove display_tracebacks option
Matthew Wild <mwild1@gmail.com>
parents: 11227
diff changeset
     4
11055
08539aa129ee util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents: 11054
diff changeset
     5
-- Library configuration (see configure())
08539aa129ee util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents: 11054
diff changeset
     6
local auto_inject_traceback = false;
08539aa129ee util.error: Add configuration for including traceback in tostring()
Matthew Wild <mwild1@gmail.com>
parents: 11054
diff changeset
     7
9750
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
local error_mt = { __name = "error" };
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
function error_mt:__tostring()
10073
6f317e51544d util.error: Fix traceback due to missing text field
Kim Alvefur <zash@zash.se>
parents: 9753
diff changeset
    11
	return ("error<%s:%s:%s>"):format(self.type, self.condition, self.text or "");
9750
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
end
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
11226
4b39691a274e util.error: rename is_err() -> is_error()
Matthew Wild <mwild1@gmail.com>
parents: 11225
diff changeset
    14
local function is_error(e)
9750
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
	return getmetatable(e) == error_mt;
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
end
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
11054
51be24b16e8a util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents: 10505
diff changeset
    18
local function configure(opt)
51be24b16e8a util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents: 10505
diff changeset
    19
	if opt.auto_inject_traceback ~= nil then
51be24b16e8a util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents: 10505
diff changeset
    20
		auto_inject_traceback = opt.auto_inject_traceback;
11228
8143fd2f138b util.error: Switch to util.debug traceback tables and remove display_tracebacks option
Matthew Wild <mwild1@gmail.com>
parents: 11227
diff changeset
    21
		if auto_inject_traceback then
12979
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 11228
diff changeset
    22
			util_debug = require "prosody.util.debug";
11228
8143fd2f138b util.error: Switch to util.debug traceback tables and remove display_tracebacks option
Matthew Wild <mwild1@gmail.com>
parents: 11227
diff changeset
    23
		end
11054
51be24b16e8a util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents: 10505
diff changeset
    24
	end
51be24b16e8a util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents: 10505
diff changeset
    25
end
51be24b16e8a util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents: 10505
diff changeset
    26
10497
d9132e7412b8 util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents: 10369
diff changeset
    27
-- Do we want any more well-known fields?
d9132e7412b8 util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents: 10369
diff changeset
    28
-- Or could we just copy all fields from `e`?
d9132e7412b8 util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents: 10369
diff changeset
    29
-- Sometimes you want variable details in the `text`, how to handle that?
d9132e7412b8 util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents: 10369
diff changeset
    30
-- Translations?
d9132e7412b8 util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents: 10369
diff changeset
    31
-- Should the `type` be restricted to the stanza error types or free-form?
d9132e7412b8 util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents: 10369
diff changeset
    32
-- What to set `type` to for stream errors or SASL errors? Those don't have a 'type' attr.
d9132e7412b8 util.error: Write down some thoughts in comments
Kim Alvefur <zash@zash.se>
parents: 10369
diff changeset
    33
11057
04ad9555c799 util.error: Add a 'source' parameter where origin module can be mentioned
Kim Alvefur <zash@zash.se>
parents: 11055
diff changeset
    34
local function new(e, context, registry, source)
11226
4b39691a274e util.error: rename is_err() -> is_error()
Matthew Wild <mwild1@gmail.com>
parents: 11225
diff changeset
    35
	if is_error(e) then return e; end
11084
ba77c142c9b1 util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents: 11083
diff changeset
    36
	local template = registry and registry[e];
ba77c142c9b1 util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents: 11083
diff changeset
    37
	if not template then
ba77c142c9b1 util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents: 11083
diff changeset
    38
		if type(e) == "table" then
ba77c142c9b1 util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents: 11083
diff changeset
    39
			template = {
ba77c142c9b1 util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents: 11083
diff changeset
    40
				code = e.code;
ba77c142c9b1 util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents: 11083
diff changeset
    41
				type = e.type;
ba77c142c9b1 util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents: 11083
diff changeset
    42
				condition = e.condition;
ba77c142c9b1 util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents: 11083
diff changeset
    43
				text = e.text;
11085
0b68697450c5 util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents: 11084
diff changeset
    44
				extra = e.extra;
11084
ba77c142c9b1 util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents: 11083
diff changeset
    45
			};
ba77c142c9b1 util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents: 11083
diff changeset
    46
		else
ba77c142c9b1 util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents: 11083
diff changeset
    47
			template = {};
ba77c142c9b1 util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents: 11083
diff changeset
    48
		end
ba77c142c9b1 util.error: Simplify error creation flow
Matthew Wild <mwild1@gmail.com>
parents: 11083
diff changeset
    49
	end
11080
505c3e5907a5 util.error: Simplify error creation - remove ability to set context from templates, and remove default context
Matthew Wild <mwild1@gmail.com>
parents: 11079
diff changeset
    50
	context = context or {};
11054
51be24b16e8a util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents: 10505
diff changeset
    51
51be24b16e8a util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents: 10505
diff changeset
    52
	if auto_inject_traceback then
11228
8143fd2f138b util.error: Switch to util.debug traceback tables and remove display_tracebacks option
Matthew Wild <mwild1@gmail.com>
parents: 11227
diff changeset
    53
		context.traceback = util_debug.get_traceback_table(nil, 2);
11054
51be24b16e8a util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents: 10505
diff changeset
    54
	end
51be24b16e8a util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents: 10505
diff changeset
    55
11081
8ea430de5fd3 util.error: Minor tweaks to error creation code to prepare for future changes
Matthew Wild <mwild1@gmail.com>
parents: 11080
diff changeset
    56
	local error_instance = setmetatable({
11079
d8fad2b48b05 util.error: Add unique 'instance_id' to error objects
Matthew Wild <mwild1@gmail.com>
parents: 11058
diff changeset
    57
		instance_id = id.short();
11081
8ea430de5fd3 util.error: Minor tweaks to error creation code to prepare for future changes
Matthew Wild <mwild1@gmail.com>
parents: 11080
diff changeset
    58
11105
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    59
		type = template.type or "cancel";
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    60
		condition = template.condition or "undefined-condition";
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    61
		text = template.text;
10505
e8186aba1583 util.error: Move default for numeric error code to net.http.server
Kim Alvefur <zash@zash.se>
parents: 10497
diff changeset
    62
		code = template.code;
11105
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    63
		extra = template.extra;
9750
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
11081
8ea430de5fd3 util.error: Minor tweaks to error creation code to prepare for future changes
Matthew Wild <mwild1@gmail.com>
parents: 11080
diff changeset
    65
		context = context;
11057
04ad9555c799 util.error: Add a 'source' parameter where origin module can be mentioned
Kim Alvefur <zash@zash.se>
parents: 11055
diff changeset
    66
		source = source;
9750
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
	}, error_mt);
11081
8ea430de5fd3 util.error: Minor tweaks to error creation code to prepare for future changes
Matthew Wild <mwild1@gmail.com>
parents: 11080
diff changeset
    68
8ea430de5fd3 util.error: Minor tweaks to error creation code to prepare for future changes
Matthew Wild <mwild1@gmail.com>
parents: 11080
diff changeset
    69
	return error_instance;
9750
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
end
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
11105
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    72
-- compact --> normal form
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    73
local function expand_registry(namespace, registry)
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    74
	local mapped = {}
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    75
	for err,template in pairs(registry) do
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    76
		local e = {
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    77
			type = template[1];
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    78
			condition = template[2];
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    79
			text = template[3];
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    80
		};
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    81
		if namespace and template[4] then
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    82
			e.extra = { namespace = namespace, condition = template[4] };
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    83
		end
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    84
		mapped[err] = e;
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    85
	end
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    86
	return mapped;
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    87
end
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    88
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    89
local function init(source, namespace, registry)
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    90
	if type(namespace) == "table" then
11211
4e060ae8520b util.error: Remove a stray word from a comment
Kim Alvefur <zash@zash.se>
parents: 11165
diff changeset
    91
		-- registry can be given as second argument if namespace is not used
11105
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    92
		registry, namespace = namespace, nil;
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    93
	end
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    94
	local _, protoerr = next(registry, nil);
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    95
	if protoerr and type(next(protoerr)) == "number" then
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    96
		registry = expand_registry(namespace, registry);
2288d206b14b util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents: 11104
diff changeset
    97
	end
11225
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11211
diff changeset
    98
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11211
diff changeset
    99
	local function wrap(e, context)
11226
4b39691a274e util.error: rename is_err() -> is_error()
Matthew Wild <mwild1@gmail.com>
parents: 11225
diff changeset
   100
		if is_error(e) then
11225
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11211
diff changeset
   101
			return e;
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11211
diff changeset
   102
		end
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11211
diff changeset
   103
		local err = new(registry[e] or {
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11211
diff changeset
   104
			type = "cancel", condition = "undefined-condition"
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11211
diff changeset
   105
		}, context, registry, source);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11211
diff changeset
   106
		err.context.wrapped_error = e;
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11211
diff changeset
   107
		return err;
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11211
diff changeset
   108
	end
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11211
diff changeset
   109
11082
fb3aec3dbe21 util.error: Have init() return an object to allow API extensibility via additional methods
Matthew Wild <mwild1@gmail.com>
parents: 11081
diff changeset
   110
	return {
11102
5b778ec095f0 util.error: Expose source and registry as fields on the registry object
Kim Alvefur <zash@zash.se>
parents: 11099
diff changeset
   111
		source = source;
5b778ec095f0 util.error: Expose source and registry as fields on the registry object
Kim Alvefur <zash@zash.se>
parents: 11099
diff changeset
   112
		registry = registry;
11082
fb3aec3dbe21 util.error: Have init() return an object to allow API extensibility via additional methods
Matthew Wild <mwild1@gmail.com>
parents: 11081
diff changeset
   113
		new = function (e, context)
fb3aec3dbe21 util.error: Have init() return an object to allow API extensibility via additional methods
Matthew Wild <mwild1@gmail.com>
parents: 11081
diff changeset
   114
			return new(e, context, registry, source);
fb3aec3dbe21 util.error: Have init() return an object to allow API extensibility via additional methods
Matthew Wild <mwild1@gmail.com>
parents: 11081
diff changeset
   115
		end;
11225
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11211
diff changeset
   116
		coerce = function (ok, err, ...)
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11211
diff changeset
   117
			if ok then
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11211
diff changeset
   118
				return ok, err, ...;
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11211
diff changeset
   119
			end
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11211
diff changeset
   120
			return nil, wrap(err);
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11211
diff changeset
   121
		end;
b0a563716334 util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents: 11211
diff changeset
   122
		wrap = wrap;
11227
b8589256b404 util.error: Expose is_error on registry objects for convenience
Matthew Wild <mwild1@gmail.com>
parents: 11226
diff changeset
   123
		is_error = is_error;
11082
fb3aec3dbe21 util.error: Have init() return an object to allow API extensibility via additional methods
Matthew Wild <mwild1@gmail.com>
parents: 11081
diff changeset
   124
	};
11058
ad07152d7bde util.error: Add a wrapper for common parameters
Kim Alvefur <zash@zash.se>
parents: 11057
diff changeset
   125
end
ad07152d7bde util.error: Add a wrapper for common parameters
Kim Alvefur <zash@zash.se>
parents: 11057
diff changeset
   126
9750
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   127
local function coerce(ok, err, ...)
11226
4b39691a274e util.error: rename is_err() -> is_error()
Matthew Wild <mwild1@gmail.com>
parents: 11225
diff changeset
   128
	if ok or is_error(err) then
9750
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   129
		return ok, err, ...;
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   130
	end
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   131
11083
1e5a0e0469de util.error: Switch coerce() to use new() and change 'native' to context field 'wrapped_error'
Matthew Wild <mwild1@gmail.com>
parents: 11082
diff changeset
   132
	local new_err = new({
1e5a0e0469de util.error: Switch coerce() to use new() and change 'native' to context field 'wrapped_error'
Matthew Wild <mwild1@gmail.com>
parents: 11082
diff changeset
   133
		type = "cancel", condition = "undefined-condition"
1e5a0e0469de util.error: Switch coerce() to use new() and change 'native' to context field 'wrapped_error'
Matthew Wild <mwild1@gmail.com>
parents: 11082
diff changeset
   134
	}, { wrapped_error = err });
9750
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   135
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   136
	return ok, new_err, ...;
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   137
end
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   138
11098
03fdf41fd948 util.error: Pass converted stanza errors throguh new()
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
   139
local function from_stanza(stanza, context, source)
11096
bd13aa89262d util.error: Collect Application-Specific Conditions from stanza errors
Kim Alvefur <zash@zash.se>
parents: 11095
diff changeset
   140
	local error_type, condition, text, extra_tag = stanza:get_error();
11093
35d2260644d9 util.error: Extract error originator from stanza errors
Kim Alvefur <zash@zash.se>
parents: 11085
diff changeset
   141
	local error_tag = stanza:get_child("error");
35d2260644d9 util.error: Extract error originator from stanza errors
Kim Alvefur <zash@zash.se>
parents: 11085
diff changeset
   142
	context = context or {};
35d2260644d9 util.error: Extract error originator from stanza errors
Kim Alvefur <zash@zash.se>
parents: 11085
diff changeset
   143
	context.stanza = stanza;
13082
6da83deb8d7f util.error: Fix error on conversion of invalid error stanza, fix #1805
Kim Alvefur <zash@zash.se>
parents: 11228
diff changeset
   144
	context.by = error_tag and error_tag.attr.by or stanza.attr.from;
11094
33b6fbdcec88 util.error: Default error originator to stanza sender
Kim Alvefur <zash@zash.se>
parents: 11093
diff changeset
   145
11099
1ea3574b19c8 util.error: Turns out <gone> wasn't alone, there's also <redirect>
Kim Alvefur <zash@zash.se>
parents: 11098
diff changeset
   146
	local uri;
1ea3574b19c8 util.error: Turns out <gone> wasn't alone, there's also <redirect>
Kim Alvefur <zash@zash.se>
parents: 11098
diff changeset
   147
	if condition == "gone" or condition == "redirect" then
1ea3574b19c8 util.error: Turns out <gone> wasn't alone, there's also <redirect>
Kim Alvefur <zash@zash.se>
parents: 11098
diff changeset
   148
		uri = error_tag:get_child_text(condition, "urn:ietf:params:xml:ns:xmpp-stanzas");
1ea3574b19c8 util.error: Turns out <gone> wasn't alone, there's also <redirect>
Kim Alvefur <zash@zash.se>
parents: 11098
diff changeset
   149
	end
1ea3574b19c8 util.error: Turns out <gone> wasn't alone, there's also <redirect>
Kim Alvefur <zash@zash.se>
parents: 11098
diff changeset
   150
11098
03fdf41fd948 util.error: Pass converted stanza errors throguh new()
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
   151
	return new({
9753
9361bd1b9c9b util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents: 9750
diff changeset
   152
		type = error_type or "cancel";
9361bd1b9c9b util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents: 9750
diff changeset
   153
		condition = condition or "undefined-condition";
9361bd1b9c9b util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents: 9750
diff changeset
   154
		text = text;
11099
1ea3574b19c8 util.error: Turns out <gone> wasn't alone, there's also <redirect>
Kim Alvefur <zash@zash.se>
parents: 11098
diff changeset
   155
		extra = (extra_tag or uri) and {
1ea3574b19c8 util.error: Turns out <gone> wasn't alone, there's also <redirect>
Kim Alvefur <zash@zash.se>
parents: 11098
diff changeset
   156
			uri = uri;
11096
bd13aa89262d util.error: Collect Application-Specific Conditions from stanza errors
Kim Alvefur <zash@zash.se>
parents: 11095
diff changeset
   157
			tag = extra_tag;
11095
4b4b5188492f util.error: Add special case handling of <gone> with an URI
Kim Alvefur <zash@zash.se>
parents: 11094
diff changeset
   158
		} or nil;
11098
03fdf41fd948 util.error: Pass converted stanza errors throguh new()
Kim Alvefur <zash@zash.se>
parents: 11096
diff changeset
   159
	}, context, nil, source);
9753
9361bd1b9c9b util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents: 9750
diff changeset
   160
end
9361bd1b9c9b util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents: 9750
diff changeset
   161
9750
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   162
return {
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   163
	new = new;
11058
ad07152d7bde util.error: Add a wrapper for common parameters
Kim Alvefur <zash@zash.se>
parents: 11057
diff changeset
   164
	init = init;
9750
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   165
	coerce = coerce;
11226
4b39691a274e util.error: rename is_err() -> is_error()
Matthew Wild <mwild1@gmail.com>
parents: 11225
diff changeset
   166
	is_error = is_error;
4b39691a274e util.error: rename is_err() -> is_error()
Matthew Wild <mwild1@gmail.com>
parents: 11225
diff changeset
   167
	is_err = is_error; -- COMPAT w/ older 0.12 trunk
9753
9361bd1b9c9b util.error: Add a function for creating an error object from an error stanza
Kim Alvefur <zash@zash.se>
parents: 9750
diff changeset
   168
	from_stanza = from_stanza;
11054
51be24b16e8a util.error: Allow optional tracebacks to be injected on errors
Matthew Wild <mwild1@gmail.com>
parents: 10505
diff changeset
   169
	configure = configure;
9750
848fd204708c util.error: Add new util library for structured errors
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   170
}