Skip to content

Commit 2b4fe22

Browse files
authored
Merge pull request #16 from xp-forge/feature/exception-cause
Include exception cause in error output
2 parents e0dc12c + d605621 commit 2b4fe22

File tree

2 files changed

+49
-29
lines changed

2 files changed

+49
-29
lines changed

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

+9-29
Original file line numberDiff line numberDiff line change
@@ -49,50 +49,31 @@ 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',
8667
strtr($e->class, '\\', '.') ?: '<main>',
8768
$e->method,
8869
$e->line,
89-
basename($e->file),
70+
$e->file ? basename($e->file) : '',
9071
$e->message ? ' - '.$e->message : ''
9172
);
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);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php namespace com\amazon\aws\lambda\unittest;
2+
3+
use lang\{IllegalArgumentException, IllegalStateException};
4+
use unittest\{Assert, Test};
5+
use xp\lambda\AwsRunner;
6+
7+
class ExceptionTest {
8+
9+
#[Test]
10+
public function includes_errorMessage() {
11+
Assert::equals(
12+
'Test',
13+
AwsRunner::error(new IllegalArgumentException('Test'))['errorMessage']
14+
);
15+
}
16+
17+
#[Test]
18+
public function includes_errorType() {
19+
Assert::equals(
20+
'lang.IllegalArgumentException',
21+
AwsRunner::error(new IllegalArgumentException('Test'))['errorType']
22+
);
23+
}
24+
25+
#[Test]
26+
public function includes_stackTrace() {
27+
Assert::true(in_array(
28+
'Exception lang.IllegalArgumentException (Test)',
29+
AwsRunner::error(new IllegalArgumentException('Test'))['stackTrace']
30+
));
31+
}
32+
33+
#[Test]
34+
public function includes_cause() {
35+
Assert::true(in_array(
36+
'Exception lang.IllegalStateException (Cause)',
37+
AwsRunner::error(new IllegalArgumentException('Test', new IllegalStateException('Cause')))['stackTrace']
38+
));
39+
}
40+
}

0 commit comments

Comments
 (0)