Skip to content

Commit

Permalink
feat: Relic server exception handler update to handle headers lazy in…
Browse files Browse the repository at this point in the history
…itialization
  • Loading branch information
klkucaj committed Dec 17, 2024
1 parent 0aaf3d4 commit d010e32
Showing 1 changed file with 54 additions and 19 deletions.
73 changes: 54 additions & 19 deletions lib/src/relic_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,42 +126,50 @@ class RelicServer {
);
}

// Parsing and converting the HTTP request to a relic request
Request relicRequest;
try {
relicRequest = Request.fromHttpRequest(
request,
strictHeaders: strictHeaders,
poweredByHeader: poweredByHeader,
);
} catch (error, stackTrace) {
}
// If the request headers are invalid, respond with a 400 Bad Request status.
on InvalidHeaderException catch (error, stackTrace) {
logMessage(
'Error parsing request headers.\n$error',
stackTrace: stackTrace,
type: LoggerType.error,
);
// Write the response to the HTTP response.
return Response.badRequest(
body: Body.fromString(error.toString()),
).writeHttpResponse(request.response);
}
// Catch any other errors.
catch (error, stackTrace) {
logMessage(
'Error parsing request.\n$error',
stackTrace: stackTrace,
type: LoggerType.error,
);

Response errorResponse;
// If the error is an [InvalidHeaderException], respond with a 400 Bad Request status.
if (error is InvalidHeaderException) {
errorResponse = Response.badRequest(
body: Body.fromString(error.toString()),
);
} else
// If the error is an [ArgumentError] with the name 'method' or 'requestedUri',
// respond with a 400 Bad Request status.
if (error is ArgumentError &&
(error.name == 'method' || error.name == 'requestedUri')) {
errorResponse = Response.badRequest();
} else {
errorResponse = Response.internalServerError();
if (error is ArgumentError) {
if (error.name == 'method' || error.name == 'requestedUri') {
return Response.badRequest().writeHttpResponse(request.response);
}
}

// Write the response to the HTTP response.
await errorResponse.writeHttpResponse(
return Response.internalServerError().writeHttpResponse(
request.response,
);
return;
}

// Handling the request with the handler
Response? response;
try {
response = await handler(relicRequest);
Expand All @@ -174,6 +182,10 @@ class RelicServer {
),
);
}
} on InvalidHeaderException catch (error) {
return Response.badRequest(
body: Body.fromString(error.toString()),
).writeHttpResponse(request.response);
} on HijackException catch (error, stackTrace) {
// If the request is already hijacked, meaning it's being handled by
// another handler, like a websocket, then don't respond with an error.
Expand All @@ -184,24 +196,47 @@ class RelicServer {
"Caught HijackException, but the request wasn't hijacked.",
stackTrace,
);
response = Response.internalServerError();
return Response.internalServerError().writeHttpResponse(
request.response,
);
} catch (error, stackTrace) {
_logError(
relicRequest,
'Error thrown by handler.\n$error',
stackTrace,
);
response = Response.internalServerError();
return Response.internalServerError().writeHttpResponse(
request.response,
);
}

// If the the request is already hijacked, meaning it's being handled by
// another handler, like a websocket, then respond with a 501 Not Implemented status.
// This should never happen, but if it does, we don't want to respond with an error.
if (!relicRequest.canHijack) {
response = Response.notImplemented();
return await Response.notImplemented().writeHttpResponse(
request.response,
);
}

await response.writeHttpResponse(request.response);
// When writing the response to the HTTP response, if the response headers
// are invalid, respond with a 400 Bad Request status.
try {
return await response.writeHttpResponse(request.response);
} on InvalidHeaderException catch (error) {
return Response.badRequest(
body: Body.fromString(error.toString()),
).writeHttpResponse(request.response);
} catch (error, stackTrace) {
_logError(
relicRequest,
'Error thrown by handler.\n$error',
stackTrace,
);
return Response.internalServerError().writeHttpResponse(
request.response,
);
}
}
}

Expand Down

0 comments on commit d010e32

Please sign in to comment.