Skip to content

Commit 8995ccf

Browse files
committed
Include exception cause in error output
1 parent 69e2bf9 commit 8995ccf

File tree

1 file changed

+8
-28
lines changed

1 file changed

+8
-28
lines changed

src/main/php/xp/lambda/AwsRunner.class.php

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,37 +49,18 @@ private static function endpoint($environment, $path) {
4949
return $c;
5050
}
5151

52-
/**
53-
* Reads a value from the given input stream
54-
*
55-
* @param io.streams.InputStream
56-
* @return var
57-
*/
58-
private static function read($in) {
59-
return Json::read(new StreamInput($in));
60-
}
61-
62-
/**
63-
* Marshals a value
64-
*
65-
* @param var $value
66-
* @return string
67-
*/
68-
private static function value($value) {
69-
return Json::of($value);
70-
}
71-
7252
/**
7353
* Marshals an error according to the AWS specification.
7454
*
7555
* @param Throwable $e
76-
* @return string
56+
* @return [:var]
7757
*/
78-
private static function error($e) {
58+
public static function error($e) {
7959
$error= ['errorMessage' => $e->getMessage(), 'errorType' => nameof($e), 'stackTrace' => []];
8060

8161
$t= XPException::wrap($e);
8262
do {
63+
$error['stackTrace'][]= $t->compoundMessage();
8364
foreach ($t->getStackTrace() as $e) {
8465
$error['stackTrace'][]= sprintf(
8566
'%s::%s(...) (line %d of %s)%s',
@@ -92,7 +73,7 @@ private static function error($e) {
9273
}
9374
} while ($t= $t->getCause());
9475

95-
return Json::of($error);
76+
return $error;
9677
}
9778

9879
/**
@@ -109,7 +90,7 @@ public static function main($args) {
10990
$lambda= self::handler($environment, Console::$out)->lambda();
11091
} catch (Throwable $t) {
11192
self::endpoint($environment, 'init/error')->post(
112-
new RequestData(self::error($t)),
93+
new RequestData(Json::of(self::error($t))),
11394
['Content-Type' => 'application/json']
11495
);
11596
return 1;
@@ -126,17 +107,16 @@ public static function main($args) {
126107

127108
$context= new Context($r->headers(), $environment);
128109
try {
129-
$event= 0 === $context->payloadLength ? null : self::read($r->in());
130-
110+
$event= 0 === $context->payloadLength ? null : Json::read(new StreamInput($r->in()));
131111
$type= 'response';
132-
$response= self::value($lambda($event, $context));
112+
$response= $lambda($event, $context);
133113
} catch (Throwable $t) {
134114
$type= 'error';
135115
$response= self::error($t);
136116
}
137117

138118
self::endpoint($environment, "invocation/{$context->awsRequestId}/{$type}")->post(
139-
new RequestData($response),
119+
new RequestData(Json::of($response)),
140120
['Content-Type' => 'application/json']
141121
);
142122
} while (true);

0 commit comments

Comments
 (0)