Postgres plv8 - 自定义错误 - 丢失自定义属性

时间:2014-01-29 13:00:32

标签: postgresql custom-errors plv8

自定义错误会丢失自定义属性

将重现问题的步骤:

创建功能

CREATE OR REPLACE FUNCTION public.utils ()
RETURNS void AS
$body$

this.dbError = function(message){
    this.message = (message || '');
};
dbError.prototype = Error.prototype;

$body$
LANGUAGE 'plv8'
VOLATILE
RETURNS NULL ON NULL INPUT
SECURITY DEFINER
COST 100;

创建触发器功能

CREATE OR REPLACE FUNCTION public.test_trigger func ()
RETURNS trigger AS
$body$

var fn = plv8.find_function('public.utils');
fn();

var err = new dbError('this is a dbError');
err.someProp = 'lalala';
throw err;

return NEW;
$body$
LANGUAGE 'plv8'
VOLATILE
CALLED ON NULL INPUT
SECURITY DEFINER
COST 100;

在anyof事件的任何表上设置此触发器并尝试执行它

DO $$ 
plv8.find_function('public.utils')();
try{
  plv8.execute('insert into Temp (field) value ($1)', [100]);
} catch(ex){
  plv8.elog(NOTICE, ex instanceof dbError);
  plv8.elog(NOTICE, ex.message);
  plv8.elog(NOTICE, ex.someProp);
}
$$ LANGUAGE plv8;

我们将看到

true
this is a dbError
undefined

预期产量是多少?

true
this is a dbError
lalala

如果我在一个范围内这样做 - 我得到了正确的结果

DO $$ 
this.dbError = function(message){
    this.message = (message || '');
};
dbError.prototype = Error.prototype;

try{
  var err = new dbError('this is a dbError');
  err.someProp = 'lalala';
  throw err;
} catch(ex){
  plv8.elog(NOTICE, ex instanceof dbError);
  plv8.elog(NOTICE, ex.message);
  plv8.elog(NOTICE, ex.someProp);
}

$$ LANGUAGE plv8;

结果:

true
this is a dbError
lalala

0 个答案:

没有答案