Skip to content

ErrorException::__construct(): Argument #1 ($contents) must be of type array, string given #82

@HelixForge

Description

@HelixForge

I'm encountering an error in the Resend PHP SDK when trying to send a password reset email in a Laravel application:

TypeError: Resend\Exceptions\ErrorException::__construct(): Argument #1 ($contents) must be of type array, string given, called in /vendor/resend/resend-php/src/Transporters/HttpTransporter.php on line 95

The issue occurs in the throwIfJsonError method in HttpTransporter.php. When a response has a Resend error name but no "error" property, the entire response is passed to the ErrorException constructor:

protected function throwIfJsonError(ResponseInterface $response, string $contents): void
{
    if ($response->getStatusCode() < 400) {
        return;
    }

    // Only handle JSON content types...
    if (! str_contains($response->getHeaderLine('Content-Type'), 'application/json')) {
        return;
    }

    try {
        $response = json_decode($contents, true, 512, JSON_THROW_ON_ERROR);

        if (
            isset($response['error']) ||
            $this->isResendError($response['name'])
        ) {
            throw new ErrorException($response['error'] ?? $response);
        }
    } catch (JsonException $jsonException) {
        throw new UnserializableResponse($jsonException);
    }
}

The problematic line is: throw new ErrorException($response['error'] ?? $response);
When $response['error'] is not set but $this->isResendError($response['name']) is true, it passes the entire $response array to the constructor.

However, the ErrorException constructor appears to expect its first argument to be specifically formatted as an array with certain properties, or it tries to handle it as a string, causing the type error.

This occurs in the password reset flow in Laravel when using the Resend mail driver. Could you please update the ErrorException constructor to properly handle both array and string inputs?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions