Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 0 additions & 90 deletions app/config/errors.php

This file was deleted.

18 changes: 13 additions & 5 deletions app/controllers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

require_once __DIR__ . '/init.php';

use OpenRuntimes\Executor\Exception;
use OpenRuntimes\Executor\ExecutionBadJsonException;
use OpenRuntimes\Executor\ExecutionBadRequestException;
use OpenRuntimes\Executor\GeneralRouteNotFoundException;
use OpenRuntimes\Executor\GeneralUnauthorizedException;
use OpenRuntimes\Executor\BodyMultipart;
use OpenRuntimes\Executor\Runner\Adapter as Runner;
use Utopia\System\System;
Expand Down Expand Up @@ -199,13 +202,13 @@ function (
// 'headers' validator
$validator = new Assoc();
if (!$validator->isValid($headers)) {
throw new Exception(Exception::EXECUTION_BAD_REQUEST, $validator->getDescription());
throw new ExecutionBadRequestException($validator->getDescription());
}

// 'variables' validator
$validator = new Assoc();
if (!$validator->isValid($variables)) {
throw new Exception(Exception::EXECUTION_BAD_REQUEST, $validator->getDescription());
throw new ExecutionBadRequestException($validator->getDescription());
}

if (in_array($payload, [null, '', '0'], true)) {
Expand Down Expand Up @@ -256,7 +259,7 @@ function (
if ($isJson) {
$executionString = \json_encode($execution, JSON_UNESCAPED_UNICODE);
if (!$executionString) {
throw new Exception(Exception::EXECUTION_BAD_JSON);
throw new ExecutionBadJsonException();
}

$response
Expand Down Expand Up @@ -286,12 +289,17 @@ function (
$response->setStatusCode(Response::STATUS_CODE_OK)->text("OK");
});

Http::wildcard()
->action(function (): void {
throw new GeneralRouteNotFoundException();
});

Http::init()
->groups(['api'])
->inject('request')
->action(function (Request $request): void {
$secretKey = \explode(' ', $request->getHeader('authorization', ''))[1] ?? '';
if ($secretKey === '' || $secretKey === '0' || $secretKey !== System::getEnv('OPR_EXECUTOR_SECRET', '')) {
throw new Exception(Exception::GENERAL_UNAUTHORIZED, 'Missing executor key');
throw new GeneralUnauthorizedException('Missing executor key');
}
});
40 changes: 25 additions & 15 deletions app/error.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use OpenRuntimes\Executor\Exception;
use OpenRuntimes\Executor\HttpException;
use Utopia\Http\Http;
use Utopia\Http\Response;
use Utopia\System\System;
Expand All @@ -9,23 +9,33 @@
->inject('error')
->inject('response')
->action(function (Throwable $error, Response $response): void {
// Show all Executor\Exceptions, or everything if in development
$public = $error instanceof Exception || Http::isDevelopment();
$exception = $public ? $error : new Exception(Exception::GENERAL_UNKNOWN);
$code = $exception->getCode() ?: 500;
if ($error instanceof HttpException) {
if ($error->publish) {
// TODO: publish to logger/Sentry
}

$output = [
'type' => $exception instanceof Exception ? $exception->getType() : Exception::GENERAL_UNKNOWN,
'message' => $exception->getMessage(),
'code' => $code,
'version' => System::getEnv('OPR_EXECUTOR_VERSION', 'unknown')
];
$code = $error->statusCode;
$output = [
'type' => $error->type,
'message' => $error->getMessage(),
'code' => $code,
'version' => System::getEnv('OPR_EXECUTOR_VERSION', 'unknown'),
];
} else {
// TODO: always publish to logger/Sentry
$code = 500;
$output = [
'type' => 'general_unknown',
'message' => Http::isDevelopment() ? $error->getMessage() : 'Internal server error.',
'code' => 500,
'version' => System::getEnv('OPR_EXECUTOR_VERSION', 'unknown'),
];
}

// If in development, include some additional details.
if (Http::isDevelopment()) {
$output['file'] = $exception->getFile();
$output['line'] = $exception->getLine();
$output['trace'] = \json_encode($exception->getTrace(), JSON_UNESCAPED_UNICODE) === false ? [] : $exception->getTrace();
$output['file'] = $error->getFile();
$output['line'] = $error->getLine();
$output['trace'] = \json_encode($error->getTrace(), JSON_UNESCAPED_UNICODE) === false ? [] : $error->getTrace();
}

$response
Expand Down
3 changes: 0 additions & 3 deletions app/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@
use Utopia\Orchestration\Adapter\DockerAPI;
use Utopia\Orchestration\Orchestration;
use Utopia\System\System;
use Utopia\Config\Config;

const MAX_LOG_SIZE = 5 * 1024 * 1024;
const MAX_BUILD_LOG_SIZE = 1000 * 1000;

Config::load('errors', __DIR__ . '/config/errors.php');

$container = new Container();

$container->set(
Expand Down
11 changes: 11 additions & 0 deletions src/Executor/CommandFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace OpenRuntimes\Executor;

class CommandFailedException extends HttpException
{
public function __construct(string $message = 'Failed to execute command.', ?\Throwable $previous = null)
{
parent::__construct(500, $message, 'command_failed', previous: $previous);
}
}
11 changes: 11 additions & 0 deletions src/Executor/CommandTimeoutException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace OpenRuntimes\Executor;

class CommandTimeoutException extends HttpException
{
public function __construct(string $message = 'Operation timed out.', ?\Throwable $previous = null)
{
parent::__construct(500, $message, 'command_timeout', previous: $previous);
}
}
94 changes: 0 additions & 94 deletions src/Executor/Exception.php

This file was deleted.

11 changes: 11 additions & 0 deletions src/Executor/ExecutionBadJsonException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace OpenRuntimes\Executor;

class ExecutionBadJsonException extends HttpException
{
public function __construct(string $message = 'Execution resulted in binary response, but JSON response does not allow binaries. Use "Accept: multipart/form-data" header to support binaries.', ?\Throwable $previous = null)
{
parent::__construct(400, $message, 'execution_bad_json', previous: $previous);
}
}
11 changes: 11 additions & 0 deletions src/Executor/ExecutionBadRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace OpenRuntimes\Executor;

class ExecutionBadRequestException extends HttpException
{
public function __construct(string $message = 'Execution request was invalid.', ?\Throwable $previous = null)
{
parent::__construct(400, $message, 'execution_bad_request', previous: $previous);
}
}
11 changes: 11 additions & 0 deletions src/Executor/ExecutionTimeoutException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace OpenRuntimes\Executor;

class ExecutionTimeoutException extends HttpException
{
public function __construct(string $message = 'Timed out waiting for execution.', ?\Throwable $previous = null)
{
parent::__construct(400, $message, 'execution_timeout', previous: $previous);
}
}
11 changes: 11 additions & 0 deletions src/Executor/GeneralRouteNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace OpenRuntimes\Executor;

class GeneralRouteNotFoundException extends HttpException
{
public function __construct(string $message = 'Not Found', ?\Throwable $previous = null)
{
parent::__construct(404, $message, 'general_route_not_found', previous: $previous);
}
}
11 changes: 11 additions & 0 deletions src/Executor/GeneralUnauthorizedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace OpenRuntimes\Executor;

class GeneralUnauthorizedException extends HttpException
{
public function __construct(string $message = 'You are not authorized to access this resource.', ?\Throwable $previous = null)
{
parent::__construct(401, $message, 'general_unauthorized', previous: $previous);
}
}
Loading
Loading