473,772 Members | 3,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error handling in stored functions/procedures

I am currently designing an application which should be accessible from
different interfaces. For this I like to be using stored procedures to
process the contents of form submissions and dialog screens.

After studying the PG database, it seems to me that I can fulfill the
following requirements.

a) The basic contents of the internal data dictionary can be used to
check incoming fields from on their length and permitted contents.

b) With a little extra work, I should be able to define a table which
can be used to check field contents against field masks.

Together with a table reference, the key/value pairs from the submission
can be checked by a table-driven stored procedure which, depending on
the outcome of the test, can return a row which contains an exit status
and an exit message.

Since I do not really like to duplicate (or rewrite functionality), the
following steps should be carried out by the database itself, which has
the possibilities to define and use them. These steps are all kinds of
constraints which can be defined in the CREATE TABLE statement itself.

The big problem I am facing however, is that when an error is
encountered by the database itself, I do not have any influence anymore
on the error message and the flow of control.

Consider the following definition :

drop table testing;
create table testing (
key_nr serial primary key,
string_value text check (string_value != 'xx'),
int_value integer check (int_value <= 255 and int_value >= 0)
);

And the following SQL statement :

insert into testing (string_value, int_value) values ('xx', 100);

This will yield the following error :

ERROR: new row for relation "testing" violates check constraint
"testing_string _value"

interrupt further processing of the stored procedure, and return
immediately to the client.

Showing a user the previous message does not really give a clue about
what has happened. With some processing it is possible to retrieve the
name of the offending field from this message, but it is impossible to
tell the user what is wrong about the contents that he filled in.

To do this would force the programmer to test and catch all errors, and
translate them to meaningful feedback for the user. Since the raised
error returns to the client, this means that the client is forced to do
one of two things : keep a local translation repository, or call another
stored procedure.

The first solution is error prone due to keeping information about parts
of the database in two different places, the second is essentially a
waste of bandwidth and time, since the problem should have been solved
in the first stored procedure.

Another possibility is of course writing all constraints in stored
procedures and not using the built-in possibilities offered by PG, but
isn't that a waste of my time and a waste of all the work that has been
done by this team ?

What solutions do you use ?

Regards,

Jurgen

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #1
5 2277
> a) The basic contents of the internal data dictionary can be used to
check incoming fields from on their length and permitted contents.

b) With a little extra work, I should be able to define a table which
can be used to check field contents against field masks.

You can use column check constraints with regular expressions
for that. No need for an extra table I would assume.

Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #2
On Sun, 30 May 2004 20:04:50 +0200
Karsten Hilbert <Ka************ *@gmx.net> wrote:
a) The basic contents of the internal data dictionary can be used to
check incoming fields from on their length and permitted contents.

b) With a little extra work, I should be able to define a table
which can be used to check field contents against field masks.

You can use column check constraints with regular expressions
for that. No need for an extra table I would assume.


Thanks for the tip.

This then, removes the first part of my explanation, and dumps me
completely in the second part, which is where the biggest problems
reside.

Jurgen

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #3
> This then, removes the first part of my explanation, and dumps me
completely in the second part, which is where the biggest problems
reside.

AFAICT 7.4 does much better error handling (no, you can't
easily control error handling inside a transaction, though). It
reports errors in a way that can be parsed a lot better thus
allowing for fairly easy translation into meaningful user
messages.

Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #4
On Sun, 30 May 2004 22:08:10 +0200
Karsten Hilbert <Ka************ *@gmx.net> wrote:
This then, removes the first part of my explanation, and dumps me
completely in the second part, which is where the biggest problems
reside.

AFAICT 7.4 does much better error handling (no, you can't
easily control error handling inside a transaction, though). It
reports errors in a way that can be parsed a lot better thus
allowing for fairly easy translation into meaningful user
messages.


You mean that the default generated error messages contain some more
information I presume, like the table name and the constraint name ?

I think I noticed that already, and since I had time since yesterday
evening, I thought things over and came to the conclusion that this
mechanism at least gives an escape hatch, since it is possible to give a
name to each constraint, and then use this name as an index to get a
proper error message.

Regards,

Jurgen

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #5
> You mean that the default generated error messages contain some more
information I presume, like the table name and the constraint name ? Even better, the information is *structured* afaik.
I think I noticed that already, and since I had time since yesterday
evening, I thought things over and came to the conclusion that this
mechanism at least gives an escape hatch, since it is possible to give a
name to each constraint, and then use this name as an index to get a
proper error message.

For check constraints you can even name them yourself during
table creation.

Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5038
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I double-checked the path to my error log. It is in /var/www/logs/php_error_log Thanks. :) -Wayne Stevenson
2
3273
by: WSeeger | last post by:
When creating a new class, is it encouraged to always include error handling routines within your LET and GET procedures? It's seems that most text books never seem to include much about error handling within classes. Just hoping to hear some programmer's thoughts on error handling.
9
10301
by: dtwilliams | last post by:
OK, i'm trying to do some error checking on stored procedures and am following the advise in Erland Sommarskog's 'Implementing Error Handling with Stored Procedures' document. Can anybody help with my stored procedures and why it keeps erroring at the '-- Create new Address Detail stage'? The errorCode value that is being return in my web app is 0, so i'm not even sure why it's even raising the error!! Rather than executing the INSERT...
0
4278
by: Rhino | last post by:
I've written several Java stored procedures now (DB2 V7.2) and I'd like to write down a few "best practices" for reference so that I will have them handy for future development. Would the experts here agree with the following? Would they add any other points? 1. If the shop standard calls for logging of application errors, a stored procedure should log any error that it encounters immediately upon detecting it and then return to the...
13
4485
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently runs into problems on the system where it will actually be used, and since I used so little error-trapping it dies very ungracefully. I will of course try to fix whatever is causing the error and add error-trapping to the functions where the...
5
3486
by: Tim Marshall | last post by:
I was following the thread "Re: Access Treeview - Is it Safe Yet?" with interest and on reading the post describing Lauren Quantrell's SmartTree, I've run into something I don't understand: Stored Procedures. I thought stored pricedures were an Oracle/MS SQL Server thing and don't know how they work with Access Jet. I've looked at some of the help on stored procedures in A2003, but really don't understand what's going on. Can someone...
1
2254
by: pob | last post by:
>From a form I have some code that calls 4 modules frmMain 1 mod 2 mod 3 mod 4 mod If mod 1 experiences an error the error handling works fine within mod 1 and writes out the error to a table, but the other modules still get
0
11599
by: Lysander | last post by:
Thought I would give something back with a few articles. This article is a bit of code to add error handling. When I have time, I want to write articles on multilingual databases, and Access Security, but I'll start with something short and simple This code was written in Access 2003 but should be valid in Access 2000 By default, when you start a new module, either in a form or report, or a global module, Access does not declare Option...
5
8840
by: Rahul B | last post by:
Hi, I have very little knowledge about creating Procedures/functions in DB2. When i tried to create the test function like CREATE FUNCTION GET_TEST (P_TEST_ID INTEGER, P_SEL_OR_SORT INTEGER,
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9912
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.