Permalink
Browse files

Use v8::Exception class when throwing exceptions.

  • Loading branch information...
1 parent 3a5cfa4 commit 34641553d0709f33fa6f3ecd200a7e42aae54bd1 @ItGacky ItGacky committed Sep 3, 2010
Showing with 51 additions and 21 deletions.
  1. +20 −7 expected/plv8.out
  2. +6 −0 plv8.cc
  3. +2 −0 plv8.h
  4. +9 −9 plv8_func.cc
  5. +14 −5 sql/plv8.sql
View
@@ -33,11 +33,11 @@ SELECT unnamed_args(ARRAY['A', 'B'], ARRAY['C', 'D']);
CREATE FUNCTION concat_strings(VARIADIC args text[]) RETURNS text AS
$$
- var result = "";
- for (var i = 0; i < args.length; i++)
- if (args[i] != null)
- result += args[i];
- return result;
+ var result = "";
+ for (var i = 0; i < args.length; i++)
+ if (args[i] != null)
+ result += args[i];
+ return result;
$$
LANGUAGE plv8 IMMUTABLE STRICT;
SELECT concat_strings('A', 'B', NULL, 'C');
@@ -192,6 +192,19 @@ SELECT record_to_text('(1,a)'::rec);
{"i":1,"t":"a"}
(1 row)
+CREATE FUNCTION return_record(i integer, t text) RETURNS record AS
+$$
+ return { "i": i, "t": t };
+$$
+LANGUAGE plv8;
+SELECT * FROM return_record(1, 'a');
+ERROR: a column definition list is required for functions returning "record"
+LINE 1: SELECT * FROM return_record(1, 'a');
+ ^
+SELECT * FROM return_record(1, 'a') AS t(j integer, s text);
+ERROR: input of anonymous composite types is not implemented
+SELECT * FROM return_record(1, 'a') AS t(x text, y text);
+ERROR: input of anonymous composite types is not implemented
CREATE FUNCTION set_of_records() RETURNS SETOF rec AS
$$
yield( { "i": 1, "t": "a" } );
@@ -227,7 +240,7 @@ LANGUAGE plv8;
SELECT test_print('ABC');
NOTICE: args = ABC
WARNING: warning
-ERROR: invalid error level for print()
+ERROR: Error: invalid error level for print()
DETAIL: test_print() LINE 4: print(20, 'ERROR is not allowed');
-- executeSql()
CREATE TABLE test_tbl (i integer, s text);
@@ -280,7 +293,7 @@ SELECT * FROM return_sql();
CREATE FUNCTION test_sql_error() RETURNS void AS $$ executeSql("ERROR") $$ LANGUAGE plv8;
SELECT test_sql_error();
-ERROR: syntax error at or near "ERROR"
+ERROR: Error: syntax error at or near "ERROR"
DETAIL: test_sql_error() LINE 1: executeSql("ERROR")
-- REPLACE FUNCTION
CREATE FUNCTION replace_test() RETURNS integer AS $$ return 1; $$ LANGUAGE plv8;
View
@@ -675,6 +675,12 @@ plv8_fill_type(plv8_type *type, Oid typid) throw()
}
}
+Handle<v8::Value>
+ThrowError(const char *message) throw()
+{
+ return ThrowException(Exception::Error(String::New(message)));
+}
+
static Handle<Context>
GetGlobalContext() throw()
{
View
2 plv8.h 100644 → 100755
@@ -103,6 +103,8 @@ class Converter
Converter& operator = (const Converter&);
};
+extern v8::Handle<v8::Value> ThrowError(const char *message) throw();
+
// plv8_type.cc
extern Datum ToDatum(v8::Handle<v8::Value> value, bool *isnull, plv8_type *type);
extern v8::Handle<v8::Value> ToValue(Datum datum, bool isnull, plv8_type *type);
View
18 plv8_func.cc 100644 → 100755
@@ -45,7 +45,7 @@ SafeCall(InvocationCallback fn, const Arguments& args) throw()
}
catch (js_error& e)
{
- return ThrowException(String::New("internal exception"));
+ return ThrowError("internal exception");
}
catch (pg_error& e)
{
@@ -56,7 +56,7 @@ SafeCall(InvocationCallback fn, const Arguments& args) throw()
FlushErrorState();
FreeErrorData(edata);
- return ThrowException(message);
+ return ThrowException(Exception::Error(message));
}
}
@@ -70,7 +70,7 @@ static Handle<v8::Value>
PrintInternal(const Arguments& args)
{
if (args.Length() < 2)
- return ThrowException(String::New("usage: print(elevel, ...)"));
+ return ThrowError("usage: print(elevel, ...)");
int elevel = args[0]->Int32Value();
switch (elevel)
@@ -86,7 +86,7 @@ PrintInternal(const Arguments& args)
case WARNING:
break;
default:
- return ThrowException(String::New("invalid error level for print()"));
+ return ThrowError("invalid error level for print()");
}
if (args.Length() == 2)
@@ -114,7 +114,7 @@ Handle<v8::Value>
ExecuteSql(const Arguments& args) throw()
{
if (args.Length() != 1 && (args.Length() != 2 || !args[1]->IsArray()))
- return ThrowException(String::New("usage: executeSql(sql [, args])"));
+ return ThrowError("usage: executeSql(sql [, args])");
Handle<v8::Value> result;
@@ -133,7 +133,7 @@ SPIResultToValue(int status)
Handle<v8::Value> result;
if (status < 0)
- return ThrowException(String::New("SPI failed"));
+ return ThrowError("SPI failed");
switch (status)
{
@@ -229,17 +229,17 @@ ExecuteSqlWithArgs(const Arguments& args)
else if (value->IsDate())
{
// TODO: Date
- return ThrowException(String::New("Date is not supported as arguments for executeSql()"));
+ return ThrowError("Date is not supported as arguments for executeSql()");
}
else if (value->IsObject())
{
// TODO: Object
- return ThrowException(String::New("Object is not supported as arguments for executeSql()"));
+ return ThrowError("Object is not supported as arguments for executeSql()");
}
else if (value->IsArray())
{
// TODO: Array
- return ThrowException(String::New("Array is not supported as arguments for executeSql()"));
+ return ThrowError("Array is not supported as arguments for executeSql()");
}
else
{
View
@@ -29,11 +29,11 @@ SELECT unnamed_args(ARRAY['A', 'B'], ARRAY['C', 'D']);
CREATE FUNCTION concat_strings(VARIADIC args text[]) RETURNS text AS
$$
- var result = "";
- for (var i = 0; i < args.length; i++)
- if (args[i] != null)
- result += args[i];
- return result;
+ var result = "";
+ for (var i = 0; i < args.length; i++)
+ if (args[i] != null)
+ result += args[i];
+ return result;
$$
LANGUAGE plv8 IMMUTABLE STRICT;
SELECT concat_strings('A', 'B', NULL, 'C');
@@ -95,6 +95,15 @@ $$
LANGUAGE plv8;
SELECT record_to_text('(1,a)'::rec);
+CREATE FUNCTION return_record(i integer, t text) RETURNS record AS
+$$
+ return { "i": i, "t": t };
+$$
+LANGUAGE plv8;
+SELECT * FROM return_record(1, 'a');
+SELECT * FROM return_record(1, 'a') AS t(j integer, s text);
+SELECT * FROM return_record(1, 'a') AS t(x text, y text);
+
CREATE FUNCTION set_of_records() RETURNS SETOF rec AS
$$
yield( { "i": 1, "t": "a" } );

0 comments on commit 3464155

Please sign in to comment.