473,769 Members | 5,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

elegant error handling in message processors, how?

hello all,

i'm writing network message processors for a server app. after
dispatching, the processor function is called for the given message
type. the tokens in the message are validated, checked against the
server state, and if everything is ok, an operation is perfomed, and
optionally a reply is sent back. in some cases of invalid input, an
error message is sent back.

with the naive approach this leads to deeply nested if statements
(complex messages), where the innermost true block does the actual
operation and sends the reply. else branches might send the error
reply. this structure is hard to oversee and maintain, so i was
searching for better ways.

A)
bool error = true;
const char* reason = "?";

do {
....
if (invalid input that needs reply) {
reason = "FOO";
break;
}
if (invalid input that doesn't need reply) {
return;
}
...
} while ((error = false));
if (error) {
send_err_reply( reason);
} else {
send_reply();
}

this is ok only when there are no validators in for/while/case
constructs. also, if called functions throw exceptions, there should be
a try/catch block, too. this brings us to plan b:

B)
try {
...
if (invalid input that needs reply) {
throw "FOO";
}
if (invalid input that doesn't need reply) {
return;
}
...
send_reply();
} catch (const char* e) {
send_err_reply( e);
} catch (...) {
// catch other exceptions from called functions and either ignore
them or send back an error
}

i'm not completely satisfied with this approach, since it uses
throw/catch in the same function, but i haven't yet found an equally
simple but more efficient solution, considering that throwing/catching
has some overhead in time/space compared to other flow control
mechanisms.

any ideas?

cheers, p

Nov 13 '06 #1
2 1622

petschy wrote:
hello all,

i'm writing network message processors for a server app. after
dispatching, the processor function is called for the given message
type. the tokens in the message are validated, checked against the
server state, and if everything is ok, an operation is perfomed, and
optionally a reply is sent back. in some cases of invalid input, an
error message is sent back.

with the naive approach this leads to deeply nested if statements
(complex messages), where the innermost true block does the actual
operation and sends the reply. else branches might send the error
reply. this structure is hard to oversee and maintain, so i was
searching for better ways.

A)
bool error = true;
const char* reason = "?";

do {
....
if (invalid input that needs reply) {
reason = "FOO";
break;
}
if (invalid input that doesn't need reply) {
return;
}
...
} while ((error = false));
if (error) {
send_err_reply( reason);
} else {
send_reply();
}

this is ok only when there are no validators in for/while/case
constructs. also, if called functions throw exceptions, there should be
a try/catch block, too. this brings us to plan b:

B)
try {
...
if (invalid input that needs reply) {
throw "FOO";
}
if (invalid input that doesn't need reply) {
return;
}
...
send_reply();
} catch (const char* e) {
send_err_reply( e);
} catch (...) {
// catch other exceptions from called functions and either ignore
them or send back an error
}

i'm not completely satisfied with this approach, since it uses
throw/catch in the same function, but i haven't yet found an equally
simple but more efficient solution, considering that throwing/catching
has some overhead in time/space compared to other flow control
mechanisms.

any ideas?

cheers, p
Have a single top level handler do all of your try/catch processing.
Then all of your handlers only have to throw exceptions and not
have to worry about try/catch blocks. The top level and only the
top level will send back the error message if it catches an exception.

Of course, the individual handlers will have to worry about being
appropriately exception safe. That is, you don't want to leave your
server in a bad state if a handler throws an exception.

Nov 13 '06 #2
Have a single top level handler do all of your try/catch processing.
Then all of your handlers only have to throw exceptions and not
have to worry about try/catch blocks. The top level and only the
top level will send back the error message if it catches an exception.

Of course, the individual handlers will have to worry about being
appropriately exception safe. That is, you don't want to leave your
server in a bad state if a handler throws an exception.
the problem with the single top-level try/catch approach is that the
message dispatch system is quite generic, and knows nothing about what
reject message would be sent with what detail info. also, the
dispatcher can't determine, whether a reject should be sent at all.
these two pieces of information is only available in the given message
processor, so must be dealt with there. of course this info could be
packed into dedicated exception objects, but this would complicate
things quite a lot, extra classes, try/catch/rethrows... also, the
message processing is transactional, and the transaction is rolled back
if an exception leaves a message processor, which aborts the sending of
the reject message among other tasks.

p

Nov 13 '06 #3

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

Similar topics

5
2052
by: steve | last post by:
Hi, In my script (phpnuke), whenever there is access to database, there is this line of code: message_die(GENERAL_ERROR, ’some error msg’, ’’, __LINE__, __FILE__, $sql); Is there a more elegant way of reporting line number besides putting this line everywhere I access db. I want to just write a function, which also globally knows about the current line number(?) and in case
6
8466
by: Squirrel | last post by:
I have a command button on a subform to delete a record. The only statement in the subroutine is: DoCmd.RunCommand acCmdDeleteRecord The subform's recordsource is "select * from tblVisit order by VisitDt" I'm getting this error message: Errno is 2465. Err.description is "Can't find field '|' referred to in your expression"
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
1908
by: mikegw | last post by:
Hello all. I am currently using an implementation of sysV shared memory. The entire shared memory is allocated is one continuous block of which I get the pointer to the head, everything should be done as offsets from this. At the moment I have two data structures, a head to a linked list which in it contains the pointer to the first element in the linked list( this is redundant as far as I can work out) and the list itself. The data...
4
1939
by: Al Williams | last post by:
Hi, I have error handling in place throughout my application. I also start the application wrapped in error handling code to catch any unexpected exceptions (i.e. exceptions that occur where I haven't placed error handling code). When I run my app from the IDE, the unhandled errors are caught by the error handling code in my Sub Main routine and the error details are logged to a text file and optionally e-mailed to me for follow-up.
9
2229
by: Gustaf | last post by:
I'm confused about structured error handling. The following piece of code is a simplification of a class library I'm working on. It works, and it does what I want, but I'm still not convinced that I have been doing it right. I think I overdo it. Please have a look: -- using System; using System.IO;
7
2364
by: Garth Wells | last post by:
I'm trying to create a DAL and am wondering what's the proper way to handle errors in this Insert method. public string Insert() { Database db = DatabaseFactory.CreateDatabase(); string sqlCommand = "pr_testx"; DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
2
8448
by: Rico | last post by:
Hello, Is there any way to get rid of or replacing the error message that pops up when you enter a combo box item that isn't in the list? I've tried using the Not In List event, but there is still an Access error that is raised. Any ideas? Thanks! Rick
15
2082
by: Charles Law | last post by:
In the build output appears ========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ========== However, the compiler does not generate any errors, and the Errors list is empty. This is an almost trivial program, but the compiler will not compile it and will not tell me where the error is.
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
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
9865
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...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5309
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
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.