Welcome Guest, Not a member yet? Register   Sign In
Exceptions::show_error() + set_status_header() + custom error codes
#1

[eluser]coolfactor[/eluser]
The Exceptions library is meant to facilitate the use of exceptions in a CodeIgniter application. And PHP-based exceptions, like many other languages, are designed to be extended and assigned custom error codes. However, with CodeIgniter, when one attempts to display one of these custom exceptions using the provided show_error() page, it may fail.

The first line in the show_error() function is to call set_status_header($code), passing an HTTP status code. So this breaks down when passing in my own custom error code. The code is not recognized, which generates a new error, displaying the text "No status text available. Please check your status code number or supply your own message text." instead of the exception message that I provided. There doesn't seem to be a way to provide additional error code-to-text mappings and this set_status_header() function can not be overridden.

Flow:
Exception handler --> show_error() --> set_status_header() --> fail.

Two solutions without any changes to CodeIgniter:
1. I could avoid using show_error() for my custom exceptions entirely and write my own method.
2. I could avoid passing in the custom error code into show_error()'s parameter and stay with a known HTTP status code.

But the fact that a "general method" for showing errors is tied into a hard-coded method for sending an HTTP status code to the browser seems to be a bit restrictive. I think the call to set_status_header() should be conditional and that it <b>need not display its own error message</b> if the code it was passed is not recognized. It should only send known HTTP status codes to the browser. Of course, this flexibility also opens up a can of worms when a custom exception code happens to be the same as a known HTTP status code... hmmm.
#2

[eluser]WanWizard[/eluser]
The status code MUST be a valid HTTP status code.

set_status_header() knows all valid codes, so if that fails, you are passing an illegal code. It is pointless wanting to extend the Exceptions class to support other status codes, as they will be invalid and should not be used.

I don't see the point of supporting (and silently ignoring) invalid status codes. Your code calls it, it should use a valid code.
#3

[eluser]coolfactor[/eluser]
I've solved my problem by adhering to the current design and not passing in my custom error codes into show_error() (solution #2 posted above). Works fine.

[quote author="WanWizard" date="1284336049"]The status code MUST be a valid HTTP status code.

set_status_header() knows all valid codes, so if that fails, you are passing an illegal code. It is pointless wanting to extend the Exceptions class to support other status codes, as they will be invalid and should not be used.

I don't see the point of supporting (and silently ignoring) invalid status codes. Your code calls it, it should use a valid code.[/quote]

The point of Exceptions (in any language) is that custom exceptions (messages and codes) be defined, usually by subclassing the built-in Exception class. Am I wrong in assuming that CodeIgniter's Exceptions class is designed to facilitate the handling of this standard exceptions approach. Seems that it would facilitate the handling of exceptions in a CodeIgniter way.

Some online resources for your reading pleasure about exceptions in different languages:
PHP - <a href="http://php.net/manual/en/language.exceptions.php">http://php.net/manual/en/language.exceptions.php</a>
C++ - <a href="http://www.cplusplus.com/doc/tutorial/exceptions/">http://www.cplusplus.com/doc/tutorial/exceptions/</a>
Java - <a href="http://www.artima.com/designtechniques/exceptions.html">http://www.artima.com/designtechniques/exceptions.html</a>

You'll notice they all follow a common design for how Exceptions should be handled.
#4

[eluser]WanWizard[/eluser]
You're not getting my point.

I don't question the need for extension of the Exceptions class. I question your usage of new codes.

The codes in question are HTTP status codes. These are defined in the different RFC's, you can't just invent new ones...
#5

[eluser]coolfactor[/eluser]
[quote author="WanWizard" date="1284380942"]You're not getting my point.

I don't question the need for extension of the Exceptions class. I question your usage of new codes.

The codes in question are HTTP status codes. These are defined in the different RFC's, you can't just invent new ones...[/quote]

HTTP errors are just one class of exceptions. Developers are free to define their own error codes when designing their Exception handling. Applications may not even be utilizing HTTP; mine isn't.

I created my original confusion by indadvertedly sending my custom error code to the browser by way of CodeIgniter's show_error() method. This method excepts the error code to be an HTTP error code. I solved my confusion by letting the method set its default error code of 500 (a standard HTTP error code).

So, in summary:

1) it's perfectly legal to define your own Exception codes. Read the links I provided above for more information.
2) I was sending a non-HTTP error code to a method that was expecting an HTTP error code. Crash!

Hope that helps.




Theme © iAndrew 2016 - Forum software by © MyBB