How to handle errors in the jpgraph class library

Source: Internet
Author: User
Tags php error
In the jpgraph tutorial, I have introduced the jpgraph installation and configuration tutorial in the php environment, the jpgraph font installation and configuration tutorial, and the jpgraph creation and graphics instance tutorial. Next, let's talk about error handling, when writing php code, errors are inevitable. The key is how to handle errors, jpgraph is a php 5-based class library. in the jpgraph tutorial, I have introduced the installation and configuration tutorial of jpgraph in the php environment, the installation and configuration tutorial of jpgraph fonts, and the tutorial of creating a graph instance in jpgraph, next, let's talk about the problem of error handling. when writing php code, errors are inevitable. The key is how to handle errors. jpgraph is a class library based on PHP5, it can use the exception handling mechanism of PHP5. The following describes the concept of error information processing in the jpgraph class library and how to handle errors in the jpgraph script.

  Introduction to jpgraph class library error handling

  Error message and image problems

Why handle the error? Because the image generation script requires a slightly different error processing method than the normal error information. To see its complexity, you need to consider how to call a jpgraph script. Usually, jpgraph scripts are called through tags. This means that the browser expects image data to be sent through the jpgraph script. If we send a text error message, the browser will not display any information.

Process the error message when the jpgraph class library normally generates an image. A typical error message is as follows:

Jpgraph error message instance

There is a rule exception, that is, the error message of header already sent. This means that the client has received a deterministic header, that is, the text header. Therefore, it is meaningless to send an image in this case. only the error message is sent as a normal text/html message.

An example of how to display the error information in a typical browser is as follows (the error information of a Chinese browser may fail, but the situation is actually the same)

Error instance sent in jpgraph header

When you get this error message, pay attention to the Red information in the figure, which will tell you which row of the file contains the error output. In most cases, it is caused by a space or tab. It may also be caused by multiple line breaks at the end of a file. These errors are difficult to detect, so you must be extremely careful when editing them in the editor.

  System cache error during image creation

Some of the problems described earlier will also generate error messages in PHP. You need to understand that any potential error messages during php jpgraph development may need to instruct the class library to intercept PHP error messages rather than converting them into image information. This is done by controlling the options INSTALL_PHP_ERR_HANDLER and CATCH_PHPERR defined in the configuration file.


If this option is set to true, a common error processor is installed on the jpgraph class library, which captures all PHP error messages and converts them to the image shown in the previous 6.1. The options are disabled by default.


This option controls whether the class library should check any generated error messages stored in the global PHP predefined variable php_errmsg. If an error occurs when calling the jpgraph script, this is very useful during development. This error is displayed as an image. It is enabled by default. Errors generated by all jpgraph class libraries are listed in Appendix H (available in English ).

  Localized error message

The jpgraph class library contains two localization errors. In version 2.5, the class library supports English and German error messages. There are two ways to choose which localization error messages to use: by defining specific global variables in the configuration file or by having three localization options in version 2.5: en, de, prod.

To specify localized error information, you need to set it in the configuration file

Define ('default _ ERR_LOCALE ', 'en ');

In addition, you can call static methods.JpGraphErr: SetErrLocale ($ aLocaleString)Dynamically set localization error messages. For example, to dynamically change to a German localized error message, you need to call

JpGraphErr: SetErrLocale ('de ');

  Precautions for jpgraph error message localization: A potential problem here is the dynamically specified language environment. Because the script dynamically calls localization error messages that occur at a certain point, this means that if the error occurs before the change occurs, the default localization error information configuration in the configuration file will be used.

  Error message on the product server

Generally, no special error information is displayed on the product server to expose potential security issues. Therefore, a pseudo error message is generated. This applies when the localized error message option is set to prod. If an error message is generated, it will be replaced by an error code. This error code will be passed to the development team for future investigation. An example of a typical prod localization error message is as follows:

Error message instance on jpgraph product server

Corresponding problem code in Appendix H

You can customize the production error information by changing the actual text in the lang/prod. inc. php file.

  Handle PHP exceptions

All exceptions in the PHP5 format can be thrown from jpgraph 2.5. The jpgraph class library provides a JpGraphException exception handling class, which is slightly different from the traditional exception classes. it not only returns a text string as an error but also an image. This is required to handle script errors and a tag is called internally. The only data that can be displayed in the browser is the image data. Therefore, it is necessary to format an error into an image.

In addition to providing an exception handling class, the jpgraph class library also installs its own default exception handling program to correctly display images. The default exception handler automatically calls an uncaptured exception at any time. This means that it is not necessary to use try {} catch () {} in the script (){}

When an exception occurs, the default exception handler first checks whether the exception is the descendant of JpGraphException. If yes, the JpgraphException: Stroke () method is called to generate an image. If the exception is not a basic exception of JpGraphException, a new error is thrown. This means that the script you created needs to call a tag internally, and all exceptions should generate a JpGraphException to generate an error image.

Typical example code for throwing a jpgraph exception

Throw new JpGraphException ('... some error message ...');

In case you need to handle exceptions, such as cleaning necessary, remember to call the Stroke () method, which will create an error message stream and return it to the browser. An example of capturing a jpgraph exception and returning it to the browser as an image is as follows:

Try {
$ Graph = new Graph ($ width, $ height );
// Jpgraph code
If (/* error condition */){
Throw new JpGraphException ('... some error message ...');
} Catch (JpGraphException $ e ){
// Perform necessary cleanup
// Return the error message
$ E-> Stroke ();

Tip: another typical exception handling method is to record the error information to the log server or a log file in text format. This operation must be performed before the Stroke () method is called.

  Selection between text and image basic error handling

In the previous section, we have introduced that when a default exception occurs, the jpgraph class library creates an error image to display the error. However, in some cases, text-based error processing is the preferred choice (usually creating a graph using the command line method ).

This can be done in two ways. First, you can capture and handle exceptions in the script or slightly modify the default behavior of the exception handler in the jpgraph class library. The method is as follows:

To enable text-based error handlers, we only need to disable image-based error handlers. This is done by callingJpGraphError: SetImageFlag ($ aFlag)Method implementation, that is

JpGraphError: SetImageFlag (false); // enable text-based error handling.

When the script is called using the command line method, adding the preceding statement to the jpgraph script will print any errors to STDERR. The command line structure used is as follows:

$> Php mygraph. php> mygraph.png

This is a very convenient way to display errors. Writing errors to STDOUT will cause the error message to be returned to the console (the above call is only redirected to STDOUT rather than STDERR ).

When the script is raised on an HTTP server (such as apache) bound to PHP, because there is no STDERR, the error message will be sent back to the browser as normal text.

  Write error messages to log files or system logs

In addition, you can also write the jpgraph error information to a named log file. The log file name must be called by calling the static methodJpGraphError: SetLogFile ($ aFileName). This file requires the PHP process to be writable. All error messages are appended to the end of the file. each error message is a date and time with a prefix (date in RFC 2822 format ). If the file name is given a syslog string, all error messages will be written to the default system log. for example, apache is generally/var/log/apache2/error_log on Unix.

  Add a new localization error message

All error messages are stored in an array with a specific index that cannot be changed. This index is used by the jpgraph class library to reference a specific error message.

To create a localized error source file, first copy the en. inc. php file to a temporary file and rename it based on the localized error information you are creating, such as the Chinese error message. Translate every error message. Make sure that the new localization error information is available when the file is stored in the jpgraph/lang/directory.

For example, if you want to create a Chinese version, copy the content in lang/en. inc. php to lang/cn. inc. php and then translate the error information. When you can use Chinese characters, use the cn string in the SettErrLocale () method.

So far, the php jpgraph class library's error information concept and how to handle jpgraph exception errors are described in the tutorial. Sorry for the poor translation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.