Error Handling

Introduction

Sajya tries to fully comply with the standards and expectations that apply to Laravel. For example, with the debug option enabled in the config/app.php configuration file, the response error information will contain much more information.

Throwing Exceptions

During the operation of your application, you can throw exceptions that will be converted into a JSON-RPC response without any intervention required. Here’s an example:

namespace App\Http\Procedures;

use Exception;
use Sajya\Server\Procedure;

class TennisProcedure extends Procedure
{
    public static string $name = 'tennis';

    public function ping()
    {
        throw new Exception('Division by zero.', -100);
    }
}

In this example, when the ping method is called, an exception is thrown with a custom message and error code. The resulting JSON-RPC response will contain the error details:

{
    "id": "1",
    "error": {
        "code": -100,
        "message": "Division by zero."
        ....
    },
    "jsonrpc": "2.0"
}

Custom Exceptions

To avoid repetition, you can create your own exception class that inherits from Sajya\Server\Exceptions\RpcException. This allows you to define the error message and code for your specific exception. Here’s how you can create a custom exception using the Artisan command:

php artisan make:exception DivisionByZero

This will generate a new exception class in the app/Exceptions directory. You can then modify this class to support JSON-RPC and define the default message and error code:

namespace App\Exceptions;

use Sajya\Server\Exceptions\RpcException;

class DivisionByZero extends RpcException
{
    /**
     * Get the default error message for the exception.
     *
     * @return string
     */
    protected function getDefaultMessage(): string
    {
        return 'Division by zero.';
    }

    /**
     * Get the default error code for the exception.
     *
     * @return int
     */
    protected function getDefaultCode(): int
    {
        return -100;
    }
}

By implementing the getDefaultMessage and getDefaultCode methods, you can define the default message and code for the exception. This allows you to provide a concise description of the error and specify an integer error code.

Throwing Custom Exceptions

Once you have defined a custom exception, you can throw it during runtime. The JSON-RPC client will receive the correct error code and description in the response. Here’s an example:

namespace App\Http\Procedures;

use App\Exceptions\DivisionByZero;
use Sajya\Server\Procedure;

class TennisProcedure extends Procedure
{
    public static string $name = 'tennis';

    public function ping()
    {
        throw new DivisionByZero();
    }
}

In this example, the ping method throws a DivisionByZero exception. The JSON-RPC response will include the correct error code and description specified in the exception class.

Reporting Exceptions

Well as in HTTP, where certain types of errors are ignored, such as 404 “not found” errors or 419 responses generated by invalid CSRF tokens. JSON-RPC has the following ignored exceptions that will not be logged:

  • Invalid params (-32602)
  • Invalid request (-32600)
  • Method not found (-32601)
  • Parse error (-32700)