This repository was archived by the owner on Sep 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathLambdaErrors.php
More file actions
96 lines (83 loc) · 2.36 KB
/
LambdaErrors.php
File metadata and controls
96 lines (83 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace LambdaErrors;
class InvocationError extends \Exception
{
}
class LambdaError extends \Exception
{
protected $errorClass;
protected $errorType;
protected $errorMessage;
protected $stackTrace;
public function __construct(\Throwable $originalError, $classification = 'Function')
{
$this->errorClass = get_class($originalError);
$this->errorType = "{$classification}<{$this->errorClass}>";
$file = $originalError->getFile();
$line = $originalError->getLine();
$message = $originalError->getMessage();
$this->errorMessage = "{$file}({$line}): {$message}";
$this->stackTrace = $this->_sanitize_stacktrace($originalError->getTraceAsString());
parent::__construct($this->errorMessage);
}
public function toLambdaResponse()
{
return [
'errorMessage' => $this->errorMessage,
'errorType' => $this->errorType,
'stackTrace' => $this->stackTrace,
];
}
public function runtimeErrorType()
{
$classification = 'Function<UserException>';
if ($this->_allowedError()) {
$classification = $this->errorType;
}
return $classification;
}
private function _sanitize_stacktrace($stacktrace)
{
$ret = [];
$safeTrace = true;
foreach (array_slice(explode(PHP_EOL, $stacktrace), 0, 100) as $trace) {
if ($safeTrace) {
[$no, $file] = explode(' ', $trace);
if (preg_match('@^/opt/lib/@', $file) === 1) {
$safeTrace = false;
} else {
$ret[] = $trace;
}
}
}
return $ret;
}
private function _allowedError()
{
return $this->_standardError();
}
private function _standardError()
{
return true; // @Todo: To determine standard exception classes.
}
}
class LambdaHandlerError extends LambdaError
{
}
class LambdaHandlerCriticalException extends LambdaError
{
}
class LambdaRuntimeError extends LambdaError
{
public function __construct($originalError)
{
parent::__construct($originalError, 'Runtime');
}
}
class LambdaRuntimeInitError extends LambdaError
{
public function __construct($originalError)
{
parent::__construct($originalError, 'Init');
}
}