Comparison

core/moduleapi.lua @ 9748:99199b53019f

core.moduleapi: Use util.error for :send_iq errors
author Kim Alvefur <zash@zash.se>
date Sun, 30 Dec 2018 16:03:15 +0100
parent 9747:c8240f931a68
child 9750:65432dc80d90
comparison
equal deleted inserted replaced
9747:c8240f931a68 9748:99199b53019f
13 local pluginloader = require "util.pluginloader"; 13 local pluginloader = require "util.pluginloader";
14 local timer = require "util.timer"; 14 local timer = require "util.timer";
15 local resolve_relative_path = require"util.paths".resolve_relative_path; 15 local resolve_relative_path = require"util.paths".resolve_relative_path;
16 local st = require "util.stanza"; 16 local st = require "util.stanza";
17 local cache = require "util.cache"; 17 local cache = require "util.cache";
18 local errutil = require "util.error";
18 local promise = require "util.promise"; 19 local promise = require "util.promise";
19 20
20 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; 21 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
21 local error, setmetatable, type = error, setmetatable, type; 22 local error, setmetatable, type = error, setmetatable, type;
22 local ipairs, pairs, select = ipairs, pairs, select; 23 local ipairs, pairs, select = ipairs, pairs, select;
365 366
366 function api:send_iq(stanza, origin, timeout) 367 function api:send_iq(stanza, origin, timeout)
367 local iq_cache = self._iq_cache; 368 local iq_cache = self._iq_cache;
368 if not iq_cache then 369 if not iq_cache then
369 iq_cache = cache.new(256, function (_, iq) 370 iq_cache = cache.new(256, function (_, iq)
370 iq.reject("evicted"); 371 iq.reject(errutil.new({
372 type = "wait", condition = "resource-constraint",
373 text = "evicted from iq tracking cache"
374 }));
371 self:unhook(iq.result_event, iq.result_handler); 375 self:unhook(iq.result_event, iq.result_handler);
372 self:unhook(iq.error_event, iq.error_handler); 376 self:unhook(iq.error_event, iq.error_handler);
373 end); 377 end);
374 self._iq_cache = iq_cache; 378 self._iq_cache = iq_cache;
375 end 379 end
391 end 395 end
392 end 396 end
393 397
394 local function error_handler(event) 398 local function error_handler(event)
395 if event.stanza.attr.from == stanza.attr.to then 399 if event.stanza.attr.from == stanza.attr.to then
396 reject(event); 400 local error_type, condition, text = event.stanza:get_error();
401 local err = errutil.new({ type = error_type, condition = condition, text = text }, event);
402 reject(err);
397 return true; 403 return true;
398 end 404 end
399 end 405 end
400 406
401 if iq_cache:get(cache_key) then 407 if iq_cache:get(cache_key) then
402 error("choose another iq stanza id attribute") 408 reject(errutil.new({
409 type = "modify", condition = "conflict",
410 text = "iq stanza id attribute already used",
411 }));
412 return;
403 end 413 end
404 414
405 self:hook(result_event, result_handler); 415 self:hook(result_event, result_handler);
406 self:hook(error_event, error_handler); 416 self:hook(error_event, error_handler);
407 417
408 local timeout_handle = self:add_timer(timeout or 120, function () 418 local timeout_handle = self:add_timer(timeout or 120, function ()
409 reject("timeout"); 419 reject(errutil.new({
420 type = "wait", condition = "remote-server-timeout",
421 text = "IQ stanza timed out",
422 }));
410 self:unhook(result_event, result_handler); 423 self:unhook(result_event, result_handler);
411 self:unhook(error_event, error_handler); 424 self:unhook(error_event, error_handler);
412 iq_cache:set(cache_key, nil); 425 iq_cache:set(cache_key, nil);
413 end); 426 end);
414 427
418 result_event = result_event, error_event = error_event, 431 result_event = result_event, error_event = error_event,
419 result_handler = result_handler, error_handler = error_handler; 432 result_handler = result_handler, error_handler = error_handler;
420 }); 433 });
421 434
422 if not ok then 435 if not ok then
423 reject("cache insertion failure"); 436 reject(errutil.new({
437 type = "wait", condition = "internal-server-error",
438 text = "Could not store IQ tracking data"
439 }));
424 return; 440 return;
425 end 441 end
426 442
427 self:send(stanza, origin); 443 self:send(stanza, origin);
428 end); 444 end);