I can't seem to find a way to retrieve error message when I throw HttpResponseException in Server side code.
Here is global exception filter I use to catch unhandled server side exceptions:
public class ExceptionHandlingAttribute : ExceptionFilterAttribute
{
private static readonly Logger Nlog = LogManager.GetCurrentClassLogger();
public override void OnException(HttpActionExecutedContext context)
{
string message = context.Exception.Message;
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent(message),
ReasonPhrase = "Exception"
});
}
}
and here is my client side code (angular2):
getAll(): Observable<T[]> {
let url_ = `${this.localUrl}`;
return this.http.request(url_
).map((response) => {
return this.processArrayResponse(response);
}).catch((response: any, caught: any) => {
console.log(response)
return this.handleArrayResponseError(response);
});
}
protected handleAnyResponseError(response: any): Observable<any> {
if (response instanceof Response) {
try {
return Observable.of(this.processAnyResponse(response));
} catch (e) {
console.log(e)
return <Observable<any>><any>Observable.throw(e);
}
} else
return <Observable<any>><any>Observable.throw(response);
}
The response I'm getting is this:
Response {_body: ArrayBuffer, status: 500, ok: false, statusText: "Exception", headers: Headers…}
headers:Headers
ok:false
status:500
statusText:"Exception"
type:2
url:"http://localhost:83/api/PreHeatSchedule/getExportFile"
_body:ArrayBuffer
__proto__: Body
I have no idea how to get the Content part from the exception in client.