カスタム エラーでカスタム プロパティが失われる
問題を再現する手順は次のとおりです。
関数を作成する
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
これを1つのスコープで行うと、正しい結果が得られます
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