aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2013-04-15 17:38:17 -0400
committerLuke Pulverenti <luke.pulverenti@gmail.com>2013-04-15 17:38:17 -0400
commit3a1317fd4fd0030c50ff7011a329ce689583d1b6 (patch)
tree656e8bde6daa5ee84d04c3ef729faff040a0a88e
parent43f7f34b5d7ac5bad34e02ca2c244c10e6f9d14e (diff)
improved http server error handling
-rw-r--r--MediaBrowser.Server.Implementations/HttpServer/HttpServer.cs42
1 files changed, 23 insertions, 19 deletions
diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpServer.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpServer.cs
index 87a01ef7d..990dc7671 100644
--- a/MediaBrowser.Server.Implementations/HttpServer/HttpServer.cs
+++ b/MediaBrowser.Server.Implementations/HttpServer/HttpServer.cs
@@ -330,6 +330,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer
throw;
}
+ finally
+ {
+ context.Response.Close();
+ }
}
/// <summary>
@@ -353,7 +357,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer
_logger.ErrorException("AcceptWebSocketAsync error", ex);
ctx.Response.StatusCode = 500;
- ctx.Response.Close();
+ }
+ finally
+ {
+ ctx.Response.Close();
}
}
@@ -386,26 +393,26 @@ namespace MediaBrowser.Server.Implementations.HttpServer
{
_logger.ErrorException("Error processing request", ex);
- response.StatusCode = statusCode;
+ // This could fail, but try to add the stack trace as the body content
+ try
+ {
+ response.StatusCode = statusCode;
- response.Headers.Add("Status", statusCode.ToString(new CultureInfo("en-US")));
+ response.Headers.Add("Status", statusCode.ToString(new CultureInfo("en-US")));
- response.Headers.Remove("Age");
- response.Headers.Remove("Expires");
- response.Headers.Remove("Cache-Control");
- response.Headers.Remove("Etag");
- response.Headers.Remove("Last-Modified");
+ response.Headers.Remove("Age");
+ response.Headers.Remove("Expires");
+ response.Headers.Remove("Cache-Control");
+ response.Headers.Remove("Etag");
+ response.Headers.Remove("Last-Modified");
- response.ContentType = "text/plain";
+ response.ContentType = "text/plain";
- if (!string.IsNullOrEmpty(ex.Message))
- {
- response.AddHeader("X-Application-Error-Code", ex.Message);
- }
+ if (!string.IsNullOrEmpty(ex.Message))
+ {
+ response.AddHeader("X-Application-Error-Code", ex.Message);
+ }
- // This could fail, but try to add the stack trace as the body content
- try
- {
var sb = new StringBuilder();
sb.AppendLine("{");
sb.AppendLine("\"ResponseStatus\":{");
@@ -415,11 +422,9 @@ namespace MediaBrowser.Server.Implementations.HttpServer
sb.AppendLine("}");
sb.AppendLine("}");
- response.StatusCode = 500;
response.ContentType = ContentType.Json;
var sbBytes = sb.ToString().ToUtf8Bytes();
response.OutputStream.Write(sbBytes, 0, sbBytes.Length);
- response.Close();
}
catch (Exception errorEx)
{
@@ -457,7 +462,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer
}
serviceStackHandler.ProcessRequest(httpReq, httpRes, operationName);
LogResponse(context, url, endPoint);
- httpRes.Close();
return;
}