MagratheaPHP2
Up to date Narrative last written —; no source changes since. Method signatures below are reflected live.

Exceptions — Error Handling

Directory: src/Exceptions/ Namespace: Magrathea2\Exceptions

MagratheaPHP2 uses a typed exception hierarchy to make error handling precise and predictable. All framework exceptions extend MagratheaException.


Exception Hierarchy

\Exception
└── MagratheaException
    ├── MagratheaApiException
    ├── MagratheaDBException
    │   └── MagratheaDBConnectionLostException
    ├── MagratheaConfigException
    └── MagratheaModelException

MagratheaException (Base)

File: src/Exceptions/MagratheaException.php

The base exception class. All framework exceptions extend this.

Properties

PropertyTypeDefaultDescription
$killerErrorboolfalseWhether this error is fatal
$msgstringException message
$_datamixednullOptional arbitrary data

Methods

SetData(mixed $data): MagratheaException

Attach arbitrary context data to the exception.

throw (new MagratheaException("Something went wrong"))
    ->SetData(["context" => "order processing", "order_id" => 42]);

GetData(): mixed

Retrieve attached data.

stackTrace(): string

Returns the formatted stack trace as a string.

getMsg(): string

Returns the exception message.

display(): void

Echo a formatted representation of the exception.

__toString(): string

Returns the exception as a string.


MagratheaApiException

File: src/Exceptions/MagratheaApiException.php Extends: MagratheaException

Thrown by API controllers and the API router. Carries an error code, an optional HTTP status and an optional data payload, making it straightforward to return structured error responses.

MagratheaApi sends it with the HTTP status from SetStatus() if it's 400-599, otherwise derived from the code: 404 → 404, 4031 → 403, 40312 → 403, anything that doesn't map to 400-599 → 500 (see MagratheaApi, "Error responses and HTTP status").

Properties

PropertyTypeDescription
$statusint|nullHTTP status set with SetStatus(); null (default): derived from the code
$codeintApplication error code

Constructor

new MagratheaApiException(
    string $message = "Magrathea Api Error",
    int $code = 0,
    ?array $data = null,
    bool $kill = true,
    ?\Exception $previous = null
)

Methods

SetStatus(int $st): MagratheaApiException

Set the HTTP status of the response, independently of the code. Only 400-599 is used; any other value is ignored and the code decides. Before 2.4.0 this was stored and never read.

// code 400 stays in the body; the HTTP status is 401
throw (new MagratheaApiException("Token missing", 400))->SetStatus(401);

$kill (constructor) doesn't affect the status. It only matters in the admin API, where a killer error is sent with ReturnApiException() and a non-killer one with the shorter ReturnFail() body. (Before 2.4.0 it set $status to 500/200, which nothing read.)

FromException(\Exception $ex, ?int $code = null, ?array $data = null): MagratheaApiException (static)

Convert any exception into a MagratheaApiException.

try {
    // something that throws a generic exception
} catch (\Exception $e) {
    throw MagratheaApiException::FromException($e, 500);
}

Common Usage in Controllers

use Magrathea2\Exceptions\MagratheaApiException;

class ProductApiControl extends MagratheaApiControl {

    public function Read($params = []): object|array {
        $id = $params["id"] ?? null;

        if (!$id) {
            throw new MagratheaApiException("Missing product ID", 400);
        }

        $product = ProductControl::GetRowWhere(["id" => $id]);

        if (!$product) {
            throw new MagratheaApiException("Product not found", 404);
        }

        return $product->ToJson();
    }
}

The API router catches MagratheaApiException and converts it to a JSON error response automatically:

{
    "success": false,
    "error": "Product not found",
    "code": 404
}

MagratheaDBException

File: src/Exceptions/MagratheaDBException.php Extends: MagratheaException

Thrown by the Database class on connection failures or query errors. Since 2.4.0, every query error is one, whatever mysqli's error reporting mode:

An API answers it with HTTP 500, whatever its code: an errno isn't an HTTP status.

Properties

PropertyTypeDescription
$querystringThe SQL query that caused the error
$valuesmixedQuery parameter values
$fullMessagestringCombined message + query context

Constructor

new MagratheaDBException(
    string $message = "Magrathea Database has failed...",
    ?string $query = null,
    int $code = 0,
    ?\Exception $previous = null
)

Methods

SetQueryData(string $query, array|string $values): MagratheaDBException

Attach the failing query and its parameters.

getFullMessage(): string

Returns the full error message including query context.

Handling DB Exceptions

use Magrathea2\Exceptions\MagratheaDBException;

try {
    Database::Instance()->Query($sql);
} catch (MagratheaDBException $e) {
    Logger::Instance()->LogError($e);
    // Return 503 or similar
}

MagratheaDBConnectionLostException

File: src/Exceptions/MagratheaDBConnectionLostException.php Extends: MagratheaDBException

Thrown by the Database query methods when the MySQL connection is gone. Its code is the MySQL errno:

CodeMeaning
2006Server has gone away: the statement was probably not executed
2013Lost connection during query: the statement may have executed
4031Disconnected by the server for inactivity (MySQL 8.0.24+, idle past wait_timeout): the statement was not executed

The failing query and values are attached ($query, $values), and the original mysqli_sql_exception, when mysqli threw one, is getPrevious(). The dead connection is already dropped when you catch it: the next query on the same Database instance opens a new connection. The framework never retries; see "Lost connections" in the Database page for when retrying is safe.

use Magrathea2\Exceptions\MagratheaDBConnectionLostException;

try {
    $job->Save();
} catch (MagratheaDBConnectionLostException $e) {
    if ($e->getCode() === 2006 || $e->getCode() === 4031) {
        $job->Save(); // the first attempt (probably) never ran
    } else {
        throw $e;     // 2013: it may have run; don't repeat a non-idempotent write
    }
}

MagratheaConfigException

File: src/Exceptions/MagratheaConfigException.php Extends: MagratheaException

Thrown when a required configuration key is missing.

// Triggered when:
Config::Instance()->GetConfigFromDefault("required_key", true);
// ↑ throws MagratheaConfigException if "required_key" is not in config

MagratheaModelException

File: src/Exceptions/MagratheaModelException.php Extends: MagratheaException

Thrown by the ORM layer when model operations fail (e.g., accessing undefined fields, loading a non-existent record).

use Magrathea2\Exceptions\MagratheaModelException;

try {
    $product = new Product();
    $product->GetById(9999); // non-existent ID
} catch (MagratheaModelException $e) {
    echo "Product not found: " . $e->getMessage();
}

Catching All Framework Exceptions

use Magrathea2\Exceptions\MagratheaException;

try {
    // any framework operation
} catch (MagratheaException $e) {
    // handles any of: API, DB, Config, Model exceptions
    Logger::Instance()->LogError($e);
    http_response_code(500);
    echo json_encode(["error" => $e->getMessage()]);
}

Global Error Handler Integration

In your entry point, you can set a global handler to catch unhandled exceptions:

set_exception_handler(function (\Throwable $e) {
    Logger::Instance()->LogError($e);

    http_response_code(500);
    header("Content-Type: application/json");
    echo json_encode(["error" => "Internal Server Error"]);
    exit;
});

Class Reference — MagratheaException

Magrathea2\Exceptions\MagratheaException extends Exception implements Stringableimplements Throwable /home/platypusweb/platypusweb.com.br/site/magratheaphp2/src/Exceptions/MagratheaException.php
GetData()
SetData($data)

Set data

ParamTypeDefault
$data mixed required
__construct($message, $code = 0, ?Exception $previous = null)
ParamTypeDefault
$message mixed required
$code mixed 0
$previous ?Exception null
display()
getMsg()
stackTrace()

Class Reference — MagratheaApiException

Magrathea2\Exceptions\MagratheaApiException extends MagratheaException implements Stringableimplements Throwable /home/platypusweb/platypusweb.com.br/site/magratheaphp2/src/Exceptions/MagratheaApiException.php

Class for Magrathea Api Errors

SetStatus($st): Magrathea2\Exceptions\MagratheaApiException

Set status

ParamTypeDefault
$st mixed required
__construct($message = "Magrathea Api Error", $code = 0, $data = null, $kill = true, ?Exception $previous = null)
ParamTypeDefault
$message mixed "Magrathea Api Error"
$code mixed 0
$data mixed null
$kill mixed true
$previous ?Exception null
static FromException(Exception $ex, $code = null, $data = null)
ParamTypeDefault
$ex Exception required
$code mixed null
$data mixed null

Class Reference — MagratheaDBException

Magrathea2\Exceptions\MagratheaDBException extends MagratheaException implements Stringableimplements Throwable /home/platypusweb/platypusweb.com.br/site/magratheaphp2/src/Exceptions/MagratheaDBException.php

Class for Magrathea DB Errors

CleanErrorMessage(string $errorMessage): string

Cleans an error message

ParamTypeDefault
$errorMessage string required
SetQueryData($query, $values): Magrathea2\Exceptions\MagratheaDBException

Adds data for debugging

ParamTypeDefault
$query mixed required
$values mixed required
__construct($message = "Magrathea Database h...", $query = null, $code = 0, ?Exception $previous = null)
ParamTypeDefault
$message mixed "Magrathea Database h..."
$query mixed null
$code mixed 0
$previous ?Exception null
getFullMessage(): string

returns the error message before cleaning stack trace

Class Reference — MagratheaConfigException

Magrathea2\Exceptions\MagratheaConfigException extends MagratheaException implements Stringableimplements Throwable /home/platypusweb/platypusweb.com.br/site/magratheaphp2/src/Exceptions/MagratheaConfigException.php

Class for Magrathea Config Errors

__construct($message = "Magrathea Config has...", $file = null, $code = 0, ?Exception $previous = null)
ParamTypeDefault
$message mixed "Magrathea Config has..."
$file mixed null
$code mixed 0
$previous ?Exception null

Class Reference — MagratheaModelException

Magrathea2\Exceptions\MagratheaModelException extends MagratheaException implements Throwableimplements Stringable /home/platypusweb/platypusweb.com.br/site/magratheaphp2/src/Exceptions/MagratheaModelException.php

Class for Magrathea Model Errors

__construct($message = "Error in Magrathea M...", $code = 0, ?Exception $previous = null)
ParamTypeDefault
$message mixed "Error in Magrathea M..."
$code mixed 0
$previous ?Exception null