clkao / plv8js-migrated

Automatically exported from code.google.com/p/plv8js
Other
0 stars 0 forks source link

Custom error error #85

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Custom error loose properties

What steps will reproduce the problem
1. create function 
CREATE OR REPLACE FUNCTION public.utils (
)
RETURNS void AS
$body$

this.dbError = function(message){
    this.message = (message || '');
};
dbError.prototype = Error.prototype;
dbError.prototype.value = function(key, value){
    if (typeof value !== 'undefined') {
    this[key] = value;
  } else {
    return this[key];
  }
};

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

2. create trigger function
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;

3. set this trigger on any table on insteadof event and try to execute it
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);
}

we will see
true
this is a dbError
undefined

What is the expected output? What do you see instead?
true
this is a dbError
lalala

What version of the product are you using? On what operating system?
PG 9.3 Windows Server R2 64

Original issue reported on code.google.com by vovapjat...@gmail.com on 28 Jan 2014 at 6:25

GoogleCodeExporter commented 9 years ago
Before string "we will see" must be "$$ LANGUAGE plv8;"

Original comment by vovapjat...@gmail.com on 28 Jan 2014 at 6:28

GoogleCodeExporter commented 9 years ago
if I do this in one scope - I get right result

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;

the result:
-----------------------
true
this is a dbError
lalala

Original comment by vovapjat...@gmail.com on 28 Jan 2014 at 6:43