Error handling

Swoft provides a complete exception and error handling mechanism. Unlike the usual fpm case, swowt divides the error into different scene types.

So you don't need to care about how to handle error response in different scenarios. You only need to implement the error handling logic of the corresponding scene. Swoft will automatically dispatch the corresponding error handler.


 composer require swoft/error 

Error scenario

The division of scene types: mainly based on the scope of the swoole callback event.

Why do you want to distinguish between divisions? In different scenarios, even if an exception or error is thrown in the same place, the processing may be different.

For example: db throws a DbException , in the http run scene you need to process a Return object. In environments such as cli, you can only process without returning any results.

Error scenario type

The scene type defined by swowt is defined in Swoft\Error\ErrorType .

 <?php declare(strict_types=1);

namespace Swoft\Error;

 * Class ErrorType
 * @since 2.0
final class ErrorType
    // Console application
    public const CLI  = 2;

    public const RPC  = 3;
    public const UDP  = 4;
    public const SOCK = 7;
    public const TASK = 8;

    public const WORKER = 9;

    // HTTP server
    public const HTTP = 16;

    // WebSocket server
    public const WS_HS  = 21;
    public const WS_OPN = 22;
    public const WS_MSG = 23;
    public const WS_CLS = 24;

    // Tcp server
    public const TCP_CNT = 31;
    public const TCP_RCV = 32;
    public const TCP_CLS = 33;

    public const SYS = 85;

    // Default error type
    public const DEF     = 90;
    public const DEFAULT = 90;

The scenarios that now support error handling are:

  • Console application
  • Http server application
  • Rpc server application
  • Tcp server application
  • Websocket server application

For specific usage of exception handling, please continue to the next document.

Git warehouse

Participate in the contribution

Welcome to participate in the contribution, you can

  • Fork our development warehouse swewt/component
  • Modify the code and then initiate the PR
  • About PR launch of Notes