Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit af434fc

Browse files
committed
Update plperl to use ereport() not elog() for user-facing messages,
so that they will be translatable. Give messages some semblance of conformance to the style guide.
1 parent 7192916 commit af434fc

File tree

2 files changed

+67
-38
lines changed

2 files changed

+67
-38
lines changed

src/pl/plperl/plperl.c

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* ENHANCEMENTS, OR MODIFICATIONS.
3434
*
3535
* IDENTIFICATION
36-
* $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.64 2004/11/24 18:47:38 tgl Exp $
36+
* $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.65 2004/11/29 20:11:05 tgl Exp $
3737
*
3838
**********************************************************************/
3939

@@ -200,7 +200,7 @@ plperl_init_interp(void)
200200

201201
plperl_interp = perl_alloc();
202202
if (!plperl_interp)
203-
elog(ERROR, "could not allocate perl interpreter");
203+
elog(ERROR, "could not allocate Perl interpreter");
204204

205205
perl_construct(plperl_interp);
206206
perl_parse(plperl_interp, plperl_init_shared_libs, 3, embedding, NULL);
@@ -233,8 +233,8 @@ plperl_safe_init(void)
233233
"$PLContainer->permit_only(':default');"
234234
"$PLContainer->share(qw[&elog &ERROR ]);"
235235
"sub ::mksafefunc { return $PLContainer->reval(qq[sub { "
236-
"elog(ERROR,'trusted perl functions disabled - "
237-
"please upgrade perl Safe module to at least 2.09');}]); }"
236+
"elog(ERROR,'trusted Perl functions disabled - "
237+
"please upgrade Perl Safe module to version 2.09 or later');}]); }"
238238
;
239239

240240
SV *res;
@@ -291,7 +291,10 @@ plperl_build_tuple_result(HV *perlhash, AttInMetadata *attinmeta)
291291
int attn = SPI_fnumber(td, key);
292292

293293
if (attn <= 0 || td->attrs[attn - 1]->attisdropped)
294-
elog(ERROR, "plperl: invalid attribute \"%s\" in hash", key);
294+
ereport(ERROR,
295+
(errcode(ERRCODE_UNDEFINED_COLUMN),
296+
errmsg("Perl hash contains nonexistent column \"%s\"",
297+
key)));
295298
if (SvTYPE(val) != SVt_NULL)
296299
values[attn - 1] = SvPV(val, PL_na);
297300
}
@@ -408,8 +411,9 @@ get_function_tupdesc(Oid result_type, ReturnSetInfo *rsinfo)
408411
if (!rsinfo || !IsA(rsinfo, ReturnSetInfo) ||
409412
rsinfo->expectedDesc == NULL)
410413
ereport(ERROR,
411-
(errcode(ERRCODE_DATATYPE_MISMATCH),
412-
errmsg("could not determine row description for function returning record")));
414+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
415+
errmsg("function returning record called in context "
416+
"that cannot accept type record")));
413417
return rsinfo->expectedDesc;
414418
}
415419
else /* ordinary composite type */
@@ -439,9 +443,13 @@ plperl_modify_tuple(HV *hvTD, TriggerData *tdata, HeapTuple otup)
439443

440444
svp = hv_fetch(hvTD, "new", 3, FALSE);
441445
if (!svp)
442-
elog(ERROR, "plperl: key \"new\" not found");
446+
ereport(ERROR,
447+
(errcode(ERRCODE_UNDEFINED_COLUMN),
448+
errmsg("$_TD->{new} does not exist")));
443449
if (SvTYPE(*svp) != SVt_RV || SvTYPE(SvRV(*svp)) != SVt_PVHV)
444-
elog(ERROR, "plperl: $_TD->{new} is not a hash reference");
450+
ereport(ERROR,
451+
(errcode(ERRCODE_DATATYPE_MISMATCH),
452+
errmsg("$_TD->{new} is not a hash reference")));
445453
hvNew = (HV *) SvRV(*svp);
446454

447455
modattrs = palloc(tupdesc->natts * sizeof(int));
@@ -455,7 +463,10 @@ plperl_modify_tuple(HV *hvTD, TriggerData *tdata, HeapTuple otup)
455463
int attn = SPI_fnumber(tupdesc, key);
456464

457465
if (attn <= 0 || tupdesc->attrs[attn - 1]->attisdropped)
458-
elog(ERROR, "plperl: invalid attribute \"%s\" in hash", key);
466+
ereport(ERROR,
467+
(errcode(ERRCODE_UNDEFINED_COLUMN),
468+
errmsg("Perl hash contains nonexistent column \"%s\"",
469+
key)));
459470
if (SvTYPE(val) != SVt_NULL)
460471
{
461472
Oid typinput;
@@ -490,7 +501,7 @@ plperl_modify_tuple(HV *hvTD, TriggerData *tdata, HeapTuple otup)
490501
pfree(modnulls);
491502

492503
if (rtup == NULL)
493-
elog(ERROR, "plperl: SPI_modifytuple failed: %s",
504+
elog(ERROR, "SPI_modifytuple failed: %s",
494505
SPI_result_code_string(SPI_result));
495506

496507
return rtup;
@@ -594,8 +605,10 @@ plperl_create_sub(char *s, bool trusted)
594605
PUTBACK;
595606
FREETMPS;
596607
LEAVE;
597-
elog(ERROR, "creation of function failed: %s",
598-
strip_trailing_ws(SvPV(ERRSV, PL_na)));
608+
ereport(ERROR,
609+
(errcode(ERRCODE_SYNTAX_ERROR),
610+
errmsg("creation of Perl function failed: %s",
611+
strip_trailing_ws(SvPV(ERRSV, PL_na)))));
599612
}
600613

601614
/*
@@ -722,8 +735,10 @@ plperl_call_perl_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo)
722735
PUTBACK;
723736
FREETMPS;
724737
LEAVE;
725-
elog(ERROR, "error from function: %s",
726-
strip_trailing_ws(SvPV(ERRSV, PL_na)));
738+
/* XXX need to find a way to assign an errcode here */
739+
ereport(ERROR,
740+
(errmsg("error from Perl function: %s",
741+
strip_trailing_ws(SvPV(ERRSV, PL_na)))));
727742
}
728743

729744
retval = newSVsv(POPs);
@@ -780,8 +795,10 @@ plperl_call_perl_trigger_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo,
780795
PUTBACK;
781796
FREETMPS;
782797
LEAVE;
783-
elog(ERROR, "error from trigger function: %s",
784-
strip_trailing_ws(SvPV(ERRSV, PL_na)));
798+
/* XXX need to find a way to assign an errcode here */
799+
ereport(ERROR,
800+
(errmsg("error from Perl trigger function: %s",
801+
strip_trailing_ws(SvPV(ERRSV, PL_na)))));
785802
}
786803

787804
retval = newSVsv(POPs);
@@ -857,7 +874,9 @@ plperl_func_handler(PG_FUNCTION_ARGS)
857874
AttInMetadata *attinmeta;
858875

859876
if (SvTYPE(perlret) != SVt_RV || SvTYPE(SvRV(perlret)) != SVt_PVAV)
860-
elog(ERROR, "plperl: set-returning function must return reference to array");
877+
ereport(ERROR,
878+
(errcode(ERRCODE_DATATYPE_MISMATCH),
879+
errmsg("set-returning Perl function must return reference to array")));
861880
ret_av = (AV *) SvRV(perlret);
862881

863882
if (SRF_IS_FIRSTCALL())
@@ -893,7 +912,9 @@ plperl_func_handler(PG_FUNCTION_ARGS)
893912
Assert(svp != NULL);
894913

895914
if (SvTYPE(*svp) != SVt_RV || SvTYPE(SvRV(*svp)) != SVt_PVHV)
896-
elog(ERROR, "plperl: element of result array is not a reference to hash");
915+
ereport(ERROR,
916+
(errcode(ERRCODE_DATATYPE_MISMATCH),
917+
errmsg("elements of Perl result array must be reference to hash")));
897918
row_hv = (HV *) SvRV(*svp);
898919

899920
tuple = plperl_build_tuple_result(row_hv, attinmeta);
@@ -913,7 +934,9 @@ plperl_func_handler(PG_FUNCTION_ARGS)
913934
FuncCallContext *funcctx;
914935

915936
if (SvTYPE(perlret) != SVt_RV || SvTYPE(SvRV(perlret)) != SVt_PVAV)
916-
elog(ERROR, "plperl: set-returning function must return reference to array");
937+
ereport(ERROR,
938+
(errcode(ERRCODE_DATATYPE_MISMATCH),
939+
errmsg("set-returning Perl function must return reference to array")));
917940
ret_av = (AV *) SvRV(perlret);
918941

919942
if (SRF_IS_FIRSTCALL())
@@ -966,7 +989,9 @@ plperl_func_handler(PG_FUNCTION_ARGS)
966989
HeapTuple tup;
967990

968991
if (SvTYPE(perlret) != SVt_RV || SvTYPE(SvRV(perlret)) != SVt_PVHV)
969-
elog(ERROR, "plperl: composite-returning function must return a reference to hash");
992+
ereport(ERROR,
993+
(errcode(ERRCODE_DATATYPE_MISMATCH),
994+
errmsg("composite-returning Perl function must return reference to hash")));
970995
perlhash = (HV *) SvRV(perlret);
971996

972997
/*
@@ -1036,7 +1061,7 @@ plperl_trigger_handler(PG_FUNCTION_ARGS)
10361061
* because SPI_finish would free it).
10371062
************************************************************/
10381063
if (SPI_finish() != SPI_OK_FINISH)
1039-
elog(ERROR, "plperl: SPI_finish() failed");
1064+
elog(ERROR, "SPI_finish() failed");
10401065

10411066
if (!(perlret && SvOK(perlret) && SvTYPE(perlret) != SVt_NULL))
10421067
{
@@ -1073,13 +1098,17 @@ plperl_trigger_handler(PG_FUNCTION_ARGS)
10731098
trigdata->tg_newtuple);
10741099
else
10751100
{
1076-
elog(WARNING, "plperl: ignoring modified tuple in DELETE trigger");
1101+
ereport(WARNING,
1102+
(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
1103+
errmsg("ignoring modified tuple in DELETE trigger")));
10771104
trv = NULL;
10781105
}
10791106
}
10801107
else
10811108
{
1082-
elog(ERROR, "plperl: expected trigger result to be undef, \"SKIP\" or \"MODIFY\"");
1109+
ereport(ERROR,
1110+
(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
1111+
errmsg("result of Perl trigger function must be undef, \"SKIP\" or \"MODIFY\"")));
10831112
trv = NULL;
10841113
}
10851114
retval = PointerGetDatum(trv);
@@ -1318,7 +1347,7 @@ compile_plperl_function(Oid fn_oid, bool is_trigger)
13181347
************************************************************/
13191348
prodesc->reference = plperl_create_sub(proc_source, prodesc->lanpltrusted);
13201349
pfree(proc_source);
1321-
if (!prodesc->reference)
1350+
if (!prodesc->reference) /* can this happen? */
13221351
{
13231352
free(prodesc->proname);
13241353
free(prodesc);

src/pl/plperl/test/test.expected

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ CREATE OR REPLACE FUNCTION perl_set() RETURNS SETOF testrowperl AS $$
119119
];
120120
$$ LANGUAGE plperl;
121121
SELECT perl_set();
122-
ERROR: plperl: element of result array is not a reference to hash
122+
ERROR: elements of Perl result array must be reference to hash
123123
SELECT * FROM perl_set();
124-
ERROR: plperl: element of result array is not a reference to hash
124+
ERROR: elements of Perl result array must be reference to hash
125125
CREATE OR REPLACE FUNCTION perl_set() RETURNS SETOF testrowperl AS $$
126126
return [
127127
{ f1 => 1, f2 => 'Hello', f3 => 'World' },
@@ -166,7 +166,7 @@ CREATE OR REPLACE FUNCTION perl_record() RETURNS record AS $$
166166
return {f2 => 'hello', f1 => 1, f3 => 'world'};
167167
$$ LANGUAGE plperl;
168168
SELECT perl_record();
169-
ERROR: could not determine row description for function returning record
169+
ERROR: function returning record called in context that cannot accept type record
170170
SELECT * FROM perl_record();
171171
ERROR: a column definition list is required for functions returning "record"
172172
SELECT * FROM perl_record() AS (f1 integer, f2 text, f3 text);
@@ -198,11 +198,11 @@ CREATE OR REPLACE FUNCTION perl_record_set() RETURNS SETOF record AS $$
198198
];
199199
$$ LANGUAGE plperl;
200200
SELECT perl_record_set();
201-
ERROR: could not determine row description for function returning record
201+
ERROR: function returning record called in context that cannot accept type record
202202
SELECT * FROM perl_record_set();
203203
ERROR: a column definition list is required for functions returning "record"
204204
SELECT * FROM perl_record_set() AS (f1 integer, f2 text, f3 text);
205-
ERROR: plperl: element of result array is not a reference to hash
205+
ERROR: elements of Perl result array must be reference to hash
206206
CREATE OR REPLACE FUNCTION perl_record_set() RETURNS SETOF record AS $$
207207
return [
208208
{ f1 => 1, f2 => 'Hello', f3 => 'World' },
@@ -211,7 +211,7 @@ CREATE OR REPLACE FUNCTION perl_record_set() RETURNS SETOF record AS $$
211211
];
212212
$$ LANGUAGE plperl;
213213
SELECT perl_record_set();
214-
ERROR: could not determine row description for function returning record
214+
ERROR: function returning record called in context that cannot accept type record
215215
SELECT * FROM perl_record_set();
216216
ERROR: a column definition list is required for functions returning "record"
217217
SELECT * FROM perl_record_set() AS (f1 integer, f2 text, f3 text);
@@ -240,45 +240,45 @@ CREATE OR REPLACE FUNCTION foo_bad() RETURNS footype AS $$
240240
return {y => 3, z => 4};
241241
$$ LANGUAGE plperl;
242242
SELECT * FROM foo_bad();
243-
ERROR: plperl: invalid attribute "z" in hash
243+
ERROR: Perl hash contains nonexistent column "z"
244244
CREATE OR REPLACE FUNCTION foo_bad() RETURNS footype AS $$
245245
return 42;
246246
$$ LANGUAGE plperl;
247247
SELECT * FROM foo_bad();
248-
ERROR: plperl: composite-returning function must return a reference to hash
248+
ERROR: composite-returning Perl function must return reference to hash
249249
CREATE OR REPLACE FUNCTION foo_bad() RETURNS footype AS $$
250250
return [
251251
[1, 2],
252252
[3, 4]
253253
];
254254
$$ LANGUAGE plperl;
255255
SELECT * FROM foo_bad();
256-
ERROR: plperl: composite-returning function must return a reference to hash
256+
ERROR: composite-returning Perl function must return reference to hash
257257
CREATE OR REPLACE FUNCTION foo_set_bad() RETURNS SETOF footype AS $$
258258
return 42;
259259
$$ LANGUAGE plperl;
260260
SELECT * FROM foo_set_bad();
261-
ERROR: plperl: set-returning function must return reference to array
261+
ERROR: set-returning Perl function must return reference to array
262262
CREATE OR REPLACE FUNCTION foo_set_bad() RETURNS SETOF footype AS $$
263263
return {y => 3, z => 4};
264264
$$ LANGUAGE plperl;
265265
SELECT * FROM foo_set_bad();
266-
ERROR: plperl: set-returning function must return reference to array
266+
ERROR: set-returning Perl function must return reference to array
267267
CREATE OR REPLACE FUNCTION foo_set_bad() RETURNS SETOF footype AS $$
268268
return [
269269
[1, 2],
270270
[3, 4]
271271
];
272272
$$ LANGUAGE plperl;
273273
SELECT * FROM foo_set_bad();
274-
ERROR: plperl: element of result array is not a reference to hash
274+
ERROR: elements of Perl result array must be reference to hash
275275
CREATE OR REPLACE FUNCTION foo_set_bad() RETURNS SETOF footype AS $$
276276
return [
277277
{y => 3, z => 4}
278278
];
279279
$$ LANGUAGE plperl;
280280
SELECT * FROM foo_set_bad();
281-
ERROR: plperl: invalid attribute "z" in hash
281+
ERROR: Perl hash contains nonexistent column "z"
282282
CREATE OR REPLACE FUNCTION perl_get_field(footype, text) RETURNS integer AS $$
283283
return $_[0]->{$_[1]};
284284
$$ LANGUAGE plperl;

0 commit comments

Comments
 (0)