phpEZ v0.2.0

DuplicateException extends DataException
in package

Exception for database constraint violations (e.g., unique, foreign key).

Thrown for duplicate key or similar constraint violations. Returns HTTP 400 (bad request) instead of 500 since it's typically a client input issue.

Tags
subpackage

db

Table of Contents

Properties

$more  : mixed

Methods

__construct()  : mixed
Initialize a duplicate constraint exception.
throw()  : never
Throw an exception with flexible named arguments.

Properties

Methods

__construct()

Initialize a duplicate constraint exception.

public __construct([string|null $msg = null ][, int|null $code = null ][, Exception|null $parent = null ][, mixed $more = null ]) : mixed
Parameters
$msg : string|null = null

Error message. Defaults to 'duplicate'.

$code : int|null = null

HTTP status code. Defaults to 400.

$parent : Exception|null = null

Previous exception for chaining.

$more : mixed = null

Additional context data for debugging.

throw()

Throw an exception with flexible named arguments.

public static throw([int|null $code = null ][, string|null $msg = null ][, Throwable|null $parent = null ][, mixed $more = null ]) : never

Static factory method for elegant exception creation. Only non-null arguments are included in the constructor call, allowing clean fluent syntax without positional confusion or needing to pass null for skipped parameters.

Parameters
$code : int|null = null

HTTP status code.

$msg : string|null = null

Error message.

$parent : Throwable|null = null

Previous exception for chaining.

$more : mixed = null

Structured error metadata.

Tags
example
// Minimal
HTTPException::throw(code: 400, msg: 'bad_request');

// With metadata
HTTPException::throw(
  code: 400,
  msg: 'validation_error',
  more: ['field' => 'email', 'reason' => 'already_exists']
);

// With exception chaining
try {
  $pdo->query($sql);
} catch (PDOException $e) {
  HTTPException::throw(code: 500, msg: 'database_error', parent: $e);
}
Return values
never

Always throws the constructed exception.

On this page

Search results