mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-22 17:47:53 +00:00
Throwing HttpInvocationException instead of generic Exception
This commit is contained in:
committed by
Steve Sanderson
parent
460f202832
commit
6d85e752e1
@@ -21,6 +21,7 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
|
|||||||
/// <seealso cref="Microsoft.AspNetCore.NodeServices.HostingModels.OutOfProcessNodeInstance" />
|
/// <seealso cref="Microsoft.AspNetCore.NodeServices.HostingModels.OutOfProcessNodeInstance" />
|
||||||
internal class HttpNodeInstance : OutOfProcessNodeInstance
|
internal class HttpNodeInstance : OutOfProcessNodeInstance
|
||||||
{
|
{
|
||||||
|
private readonly static int streamBufferSize = 16 * 1024;
|
||||||
private static readonly Regex PortMessageRegex =
|
private static readonly Regex PortMessageRegex =
|
||||||
new Regex(@"^\[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on port (\d+)\]$");
|
new Regex(@"^\[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on port (\d+)\]$");
|
||||||
|
|
||||||
@@ -67,8 +68,10 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
|
|||||||
if (!response.IsSuccessStatusCode)
|
if (!response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
// Unfortunately there's no true way to cancel ReadAsStringAsync calls, hence AbandonIfCancelled
|
// Unfortunately there's no true way to cancel ReadAsStringAsync calls, hence AbandonIfCancelled
|
||||||
var responseErrorString = await response.Content.ReadAsStringAsync().OrThrowOnCancellation(cancellationToken);
|
var responseJson = await response.Content.ReadAsStringAsync().OrThrowOnCancellation(cancellationToken);
|
||||||
throw new Exception("Call to Node module failed with error: " + responseErrorString);
|
var responseError = JsonConvert.DeserializeObject<RpcJsonResponse>(responseJson, jsonSerializerSettings);
|
||||||
|
|
||||||
|
throw new NodeInvocationException(responseError.ErrorMessage, responseError.ErrorDetails);
|
||||||
}
|
}
|
||||||
|
|
||||||
var responseContentType = response.Content.Headers.ContentType;
|
var responseContentType = response.Content.Headers.ContentType;
|
||||||
@@ -136,5 +139,35 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
|
|||||||
_disposed = true;
|
_disposed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static async Task<T> ReadJsonAsync<T>(Stream stream, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var json = Encoding.UTF8.GetString(await ReadAllBytesAsync(stream, cancellationToken));
|
||||||
|
return JsonConvert.DeserializeObject<T>(json, jsonSerializerSettings);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task<byte[]> ReadAllBytesAsync(Stream input, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
byte[] buffer = new byte[streamBufferSize];
|
||||||
|
|
||||||
|
using (var ms = new MemoryStream())
|
||||||
|
{
|
||||||
|
int read;
|
||||||
|
while ((read = await input.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) > 0)
|
||||||
|
{
|
||||||
|
ms.Write(buffer, 0, read);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ms.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma warning disable 649 // These properties are populated via JSON deserialization
|
||||||
|
private class RpcJsonResponse
|
||||||
|
{
|
||||||
|
public string ErrorMessage { get; set; }
|
||||||
|
public string ErrorDetails { get; set; }
|
||||||
|
}
|
||||||
|
#pragma warning restore 649
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,5 +86,8 @@ function readRequestBodyAsJson(request, callback) {
|
|||||||
|
|
||||||
function respondWithError(res: http.ServerResponse, errorValue: any) {
|
function respondWithError(res: http.ServerResponse, errorValue: any) {
|
||||||
res.statusCode = 500;
|
res.statusCode = 500;
|
||||||
res.end(errorValue.stack || errorValue.toString());
|
res.end(JSON.stringify({
|
||||||
|
errorMessage: errorValue.message || errorValue,
|
||||||
|
errorDetails: errorValue.stack || null
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user