postal-php icon indicating copy to clipboard operation
postal-php copied to clipboard

Postal server's error response code is incompatible with Exception

Open craigrileyuk opened this issue 2 years ago • 2 comments

Postal Server: v2.1.4 (self-hosted) Postal-PHP: v2.0.0

When sending an email via the PHP Client that returns an error, the code property of data is passed through to ApiException which in turn extends the PHP base class Exception.

Exception, though, requires the 2nd argument ($code) to be an integer, whereas the postal-server returns a string.

For example:

^ array:4 [▼
  "status" => "error"
  "time" => 0.01
  "flags" => []
  "data" => array:2 [▼
    "code" => "UnauthenticatedFromAddress"
    "message" => "The From address is not authorised to send mail from this server"
  ]
]

is received to the $json variable on vendor/postal/postal/src/Client.php::65

Line 73 - 79 will then do the following:

$code = 0;
if (isset($json['data']['code'])) {
    $message = $json['data']['code'] . ': ' . $message;
    $code = $json['data']['code'];
}

throw new ApiException($message, $code);

At this point, $code equals UnauthenticatedFromAddress and so causes a type error when passed to ApiException extends Exception since its signature is

public __construct(string $message = "", int $code = 0, ?Throwable $previous = null)

This results in the error failing to throw, instead causing a TypeError: Exception::__construct(): Argument #2 ($code) must be of type int, string given in file vendor/postal/postal/src/Client.php on line 79

craigrileyuk avatar Dec 18 '23 16:12 craigrileyuk

Ah excellent observation, I'll have a look at this tomorrow.

willpower232 avatar Dec 18 '23 17:12 willpower232

Fixed in e361d11aadb724a482092784ebfe00461c492b5d and released

willpower232 avatar Dec 19 '23 09:12 willpower232