3

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

哪些步骤将重现该问题:

创建函数

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;

在任何表上设置此触发器而不是事件并尝试执行它

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
4

0 回答 0