Skip to content

Exception handling improvements #130

@benjamin-smith

Description

@benjamin-smith

Right now, if an exception is thrown while processing an Element API HTTP request, the following code block is triggered:

} catch (\Throwable $e) {
$data = [
'error' => [
'code' => $e instanceof HttpException ? $e->statusCode : $e->getCode(),
'message' => $e->getMessage(),
]
];
$statusCode = $e instanceof HttpException ? $e->statusCode : 500;
$statusText = $e->getMessage();
}

  • The error message is output to the client, regardless of the devMode setting. I've got info leaking from my API that I would like to keep private for security reasons.
  • The response is cached, regardless if an error occurred or not (I can see situations where not caching the error would also be bad, and could drive up server load, but overall my personal expectation would be this is not cached so temporary errors such as failed external network requests don't break an API for an extended period of time):
    // Cache it?
    if ($cache) {
    if ($cache !== true) {
    $expire = ConfigHelper::durationInSeconds($cache);
    } else {
    $expire = null;
    }
    /** @noinspection PhpUndefinedVariableInspection */
    $cacheService->set($cacheKey, $response->content, $expire);
    }

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