Jump to content

How To Handle Business Logic Errors Effectively?


alwebtech

Recommended Posts

Let's suppose we have some objects hierarchy built via composition. Is there any pattern to register business logic errors within inner objects and pass them to the upper objects ?

 

I am just tired of all these addErrors, getErrors on each level and I feel that I do something incorrectly.

 

I know about exceptions, but I have no mind how to use them in such case. Business logic error is technically not a critical situation, which should result in interruption of the program execution. Actually interruption is not supposed at all, because we need to register the error and just move on.

 

Thanks in advance to share your wisdom and knowledge )

 

Small snippet to illustrate the problem:

 

<?php
class ErrorContainer {
 private $errors = array();
 function addError($error) { if (!is_array($error)) { $this->errors[] = $error;} else { $this->errors = array_merge($this->errors, $error); } }
 function getErrors() { return $this->errors[]; }
 function hasErrors() { return !empty($this->errors); }
}

class Processor extends ErrorContainer {
 function process($account_id, $orders) {
	 $account = new Account();
	 if (!$account->loadById($account_id)) { $this->addErrors($account->getErrors);}

	 foreach ($orders as $order_id) {
		 $order = new Order();
		 if (!$order->loadById($order_id)) { $this->addErrors($order->getErrors);}
	 }
 }

 return $this->hasErrors();
}

class Account extends ErrorContainer {
 function loadById($account_id) {
	 $account = select_from_database($account_id);
	 if (!$account) { $this->addError("Account is missing"); }
	 if (!$account['active']) { $this->addError("Account is inactive"); }
	 // and so on, some checks may add errors in a cycle

	 return $this->hasErrors();
 }
}

class Order extends ErrorContainer {} // very similar to Account, but has its own checks

//Usage:

$errors = array();
$items = load_items_from_xml($xml);
foreach ($items as $item) {
 $processor = new Processor();
 if (!$processor->process($item['account_id'], $item['orders'])) {
	 $errors = array_merge($errors, $processor->getErrors());
 }
}

Link to comment
Share on other sites

What I've done is to register my own error handler, and given business logic errors the level of E_USER_WARNINGS. Which allows me to handle them properly from within the error handler, by using the template engine or whatever other methods necessary. That said, you also need to handle some of the stuff inside the code that triggers the error, in most cases.

 

Most people seem to be using Exceptions for this kind of thing. Something which I, as you, don't quite agree with. Exceptions are (by their name even) meant for exceptional events only, something which business logic errors hardly are.

I can see the immediate allure with using them, as they are quite efficient at bubbling up the layers. That said, they also require some extra code to handle properly. Something which, apparently, makes most people just use them as quick and dirty methods to bubble an error up to the/a main function that handles everything. Just like a custom error handler...

 

So, yeah, I'd look at registering a custom error handler if I were you. Or even several, if need be. ;)

Edited by Christian F.
Link to comment
Share on other sites

What I've done is to register my own error handler, and given business logic errors the level of E_USER_WARNINGS. Which allows me to handle them properly from within the error handler, by using the template engine or whatever other methods necessary. That said, you also need to handle some of the stuff inside the code that triggers the error, in most cases.

 

Most people seem to be using Exceptions for this kind of thing. Something which I, as you, don't quite agree with. Exceptions are (by their name even) meant for exceptional events only, something which business logic errors hardly are.

I can see the immediate allure with using them, as they are quite efficient at bubbling up the layers. That said, they also require some extra code to handle properly. Something which, apparently, makes most people just use them as quick and dirty methods to bubble an error up to the/a main function that handles everything. Just like a custom error handler...

 

So, yeah, I'd look at registering a custom error handler if I were you. Or even several, if need be. ;)

 

Thanks for your opinion, Christian. Custom error handler is a nice idea, I will see what I can do in this direction.

Link to comment
Share on other sites

You're welcome, and I wish you good luck!

 

If you have any further questions, you know where to ask. ;)

 

PS: This should probably have been posted in "Application design", as it's more of a design question than a problematic PHP code question.

Edited by Christian F.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.