error handling #1749

This commit is contained in:
Jamie 2017-11-29 11:55:12 +00:00
parent ebb3e916b6
commit f944ef6a79
5 changed files with 224 additions and 174 deletions

View file

@ -43,10 +43,7 @@ namespace Ombi.Schedule.Jobs.Plex
var plexEpisodes = _repo.GetAllEpisodes().Include(x => x.Series);
foreach (var child in tv)
{
PlexServerContent item = null;
var useImdb = false;
{ var useImdb = false;
var useTvDb = false;
if (child.ParentRequest.ImdbId.HasValue())
{

View file

@ -13,54 +13,54 @@ export class IdentityService extends ServiceAuthHelpers {
super(http, "/api/v1/Identity/", platformLocation);
}
public createWizardUser(user: ICreateWizardUser): Observable<boolean> {
return this.regularHttp.post(`${this.url}Wizard/`, JSON.stringify(user), { headers: this.headers }).map(this.extractData);
return this.regularHttp.post(`${this.url}Wizard/`, JSON.stringify(user), { headers: this.headers }).map(this.extractData).catch(this.handleError);
}
public getUser(): Observable<IUser> {
return this.http.get(this.url).map(this.extractData);
return this.http.get(this.url).map(this.extractData).catch(this.handleError);
}
public getUserById(id: string): Observable<IUser> {
return this.http.get(`${this.url}User/${id}`).map(this.extractData);
return this.http.get(`${this.url}User/${id}`).map(this.extractData).catch(this.handleError);
}
public getUsers(): Observable<IUser[]> {
return this.http.get(`${this.url}Users`).map(this.extractData);
return this.http.get(`${this.url}Users`).map(this.extractData).catch(this.handleError);
}
public getAllAvailableClaims(): Observable<ICheckbox[]> {
return this.http.get(`${this.url}Claims`).map(this.extractData);
return this.http.get(`${this.url}Claims`).map(this.extractData).catch(this.handleError);
}
public createUser(user: IUser): Observable<IIdentityResult> {
return this.http.post(this.url, JSON.stringify(user), { headers: this.headers }).map(this.extractData);
return this.http.post(this.url, JSON.stringify(user), { headers: this.headers }).map(this.extractData).catch(this.handleError);
}
public updateUser(user: IUser): Observable<IIdentityResult> {
return this.http.put(this.url, JSON.stringify(user), { headers: this.headers }).map(this.extractData);
return this.http.put(this.url, JSON.stringify(user), { headers: this.headers }).map(this.extractData).catch(this.handleError);
}
public updateLocalUser(user: IUpdateLocalUser): Observable<IIdentityResult> {
return this.http.put(this.url + "local", JSON.stringify(user), { headers: this.headers }).map(this.extractData);
return this.http.put(this.url + "local", JSON.stringify(user), { headers: this.headers }).map(this.extractData).catch(this.handleError);
}
public deleteUser(user: IUser): Observable<IIdentityResult> {
return this.http.delete(`${this.url}${user.id}`, { headers: this.headers }).map(this.extractData);
return this.http.delete(`${this.url}${user.id}`, { headers: this.headers }).map(this.extractData).catch(this.handleError);
}
public hasUserRequested(userId: string): Observable<boolean> {
return this.http.get(`${this.url}userhasrequest/${userId}`).map(this.extractData);
return this.http.get(`${this.url}userhasrequest/${userId}`).map(this.extractData).catch(this.handleError);
}
public submitResetPassword(email: string): Observable<IIdentityResult> {
return this.regularHttp.post(this.url + "reset", JSON.stringify({email}), { headers: this.headers }).map(this.extractData);
return this.regularHttp.post(this.url + "reset", JSON.stringify({email}), { headers: this.headers }).map(this.extractData).catch(this.handleError);
}
public resetPassword(token: IResetPasswordToken): Observable<IIdentityResult> {
return this.regularHttp.post(this.url + "resetpassword", JSON.stringify(token), { headers: this.headers }).map(this.extractData);
return this.regularHttp.post(this.url + "resetpassword", JSON.stringify(token), { headers: this.headers }).map(this.extractData).catch(this.handleError);
}
public sendWelcomeEmail(user: IUser): Observable<null> {
return this.http.post(`${this.url}welcomeEmail`, JSON.stringify(user), { headers: this.headers }).map(this.extractData);
return this.http.post(`${this.url}welcomeEmail`, JSON.stringify(user), { headers: this.headers }).map(this.extractData).catch(this.handleError);
}
public hasRole(role: string): boolean {

View file

@ -1,7 +1,6 @@
import { PlatformLocation } from "@angular/common";
import { Headers, Http, Response } from "@angular/http";
import "rxjs/add/observable/throw";
import { Observable } from "rxjs/Observable";
import { AuthHttp } from "angular2-jwt";
@ -24,12 +23,17 @@ export class ServiceHelpers {
return body;
}
protected handleError(error: any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
const errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : "Server error";
return Observable.throw(errMsg);
protected handleError(error: Response | any) {
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || "";
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ""} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return errMsg;
}
}
@ -63,11 +67,16 @@ export class ServiceAuthHelpers {
}
}
protected handleError(error: any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
const errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : "Server error";
return Observable.throw(errMsg);
protected handleError(error: Response | any) {
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || "";
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ""} ${err}`;
} else {
errMsg = error.Message ? error.message : error.toString();
}
console.error(errMsg);
return errMsg;
}
}

View file

@ -0,0 +1,43 @@
using System;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
namespace Ombi
{
public class ErrorHandlingMiddleware
{
private readonly RequestDelegate next;
public ErrorHandlingMiddleware(RequestDelegate next)
{
this.next = next;
}
public async Task Invoke(HttpContext context /* other scoped dependencies */)
{
try
{
await next(context);
}
catch (Exception ex)
{
await HandleExceptionAsync(context, ex);
}
}
private static Task HandleExceptionAsync(HttpContext context, Exception exception)
{
var code = HttpStatusCode.InternalServerError; // 500 if unexpected
//if (exception is NotFoundException) code = HttpStatusCode.NotFound;
if (exception is UnauthorizedAccessException) code = HttpStatusCode.Unauthorized;
var result = JsonConvert.SerializeObject(new { error = exception.Message });
context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)code;
return context.Response.WriteAsync(result);
}
}
}

View file

@ -202,6 +202,7 @@ namespace Ombi
c.ShowJsonEditor();
});
app.UseMiddleware<ErrorHandlingMiddleware>();
app.UseMvc(routes =>
{
routes.MapRoute(