Systematization of error codes for a web application in php?

I am working on a class based web application. I have places where objects interact, and I have certain situations where I use error codes to communicate with the end user - usually when form values ​​are missing or invalid. These are situations where exceptions are unfounded (and I'm not sure that I could avoid situations with exceptions anyway).

In one object, I have 20 code numbers, each of which corresponds to a message addressed to the user and a message addressed to the administrator / developer, so both sides know what is happening. Now, when I worked on the code several times, I found it difficult to quickly figure out which code numbers in the series that I already used, so I accidentally create conflicting codes. For example, I just did it today from 12, 13, 14, and 15.

How can I better organize this so as not to create conflicting error codes? Should I create one single-element class errorCodes, which has a main list of all error codes for all classes, organizing them throughout the web application? Or should each object have its own set of error codes, when necessary, and I just keep the list in the comments for the object in order to use and update it when I go?


Edit: Therefore, I like the suggestions to use constants or named constants inside a class. This gives me one place where I programmatically identify and track error codes and their messages.

The next question: what interface do I provide to the outside world for error codes and messages of this class? Am I doing something like triggerError(20)in a class and then providing a public method to return an error code, a string constant, and a message related to the user and the administrator?

+5
source share
4 answers

You can create a pair definesto create Named Constants for all error codes:

define('ERROR_CODE_SQL_QUERY', 1);
define('ERROR_CODE_PAGE_NOT_FOUND', 2);
define('ERROR_CODE_NOT_ALLOWED', 3);
// and so on

And then use constants in your code:

if ($errorCode == ERROR_CODE_SQL_QUERY) {
    // deal with SQL errors
}


In this case, you will not use a numerical value anywhere in your code: everywhere (except for the attached and the only file in which you put defines), you will use the codes.

It means:

  • ,
  • , , , ,
  • , .


:

class Error {
    const CODE_SQL_QUERY = 1;
    const CODE_PAGE_NOT_FOUND = 2;
    const CODE_NOT_ALLOWED = 3;

    // Add some methods here, if needed
}

- :

if ($errorCode == Error::CODE_SQL_QUERY) {
    // deal with SQL errors
}


?

, ... , . Else, .

+7

, ?

class MyErrorProneClass { 
    const TURNED_INTO_A_NEWT = 12;
    ...

    public function dontBurnMe() {
        // echo your error here using self::TURNED_INTO_A_NEWT
}

, , , . - , .

. (__FILE__ __LINE__ ), .

, .

,


Edit:

:

class MyErrorProneClass { 
    protected static $turnedIntoANewt = 12;
    ...

    public function dontBurnMe() {
        // echo your error here using self::$turnedIntoANewt
}

, , . , , :

MyErrorProneClass::TURNED_INTO_A_NEWT

( , ) ( /) . , .

+1

, , trigger_error(), , .

0

exceptions? , , , .

, /.

0

All Articles