Skip to content

Commit ff15933

Browse files
committed
add exception process
1 parent 882242e commit ff15933

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

src/StreamingServer.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,12 @@ function ($error) use ($that, $conn, $request) {
294294
$exception = new \RuntimeException($message, null, $previous);
295295

296296
$that->emit('error', array($exception));
297-
return $that->writeError($conn, 500, $request);
297+
//return $that->writeError($conn, 500, $request);
298+
try {
299+
return $that->fcWriteError($conn, $error, $request);
300+
} catch (Throwable $e) {
301+
return $that->writeError($conn, 500, $request);
302+
}
298303
}
299304
);
300305
}
@@ -320,6 +325,53 @@ public function writeError(ConnectionInterface $conn, $code, ServerRequestInterf
320325

321326
$this->handleResponse($conn, $request, $response);
322327
}
328+
329+
/** @internal */
330+
public function fcWriteError(ConnectionInterface $conn, $e, ServerRequestInterface $request) {
331+
// filter out the server information in trace
332+
$traceStr = $e->getTraceAsString();
333+
$pos = strpos($traceStr, "/var/fc/runtime/php7/src/server.php");
334+
$traceStr = substr($traceStr, 0, $pos);
335+
$err = array(
336+
"errorMessage" => $e->getMessage(),
337+
"errorType" => get_class($e),
338+
"stackTrace" => array(
339+
"file" => $e->getFile(),
340+
"line" => $e->getLine(),
341+
"traceString" => $traceStr,
342+
),
343+
);
344+
345+
var_export($err);
346+
if (ob_get_length()) ob_clean();
347+
echo FC_LOG_TAIL_END_PREFIX . $GLOBALS['requestId'] . PHP_EOL;
348+
unset($GLOBALS['requestId']);
349+
350+
$errStr = json_encode($err, JSON_UNESCAPED_UNICODE);
351+
$response = new Response(
352+
404,
353+
array(
354+
'Content-Type' => 'application/octet-stream',
355+
'Content-Length' => strlen($errStr),
356+
'Connection' => 'keep-alive',
357+
),
358+
$errStr
359+
);
360+
361+
// append reason phrase to response body if known
362+
$reason = $response->getReasonPhrase();
363+
if ($reason !== '') {
364+
$body = $response->getBody();
365+
$body->seek(0, SEEK_END);
366+
$body->write(': ' . $reason);
367+
}
368+
369+
if ($request === null) {
370+
$request = new ServerRequest('GET', '/', array(), null, '1.1');
371+
}
372+
373+
$this->handleResponse($conn, $request, $response);
374+
}
323375

324376

325377
/** @internal */

0 commit comments

Comments
 (0)