This commit is contained in:
henrydays 2018-08-26 15:18:56 +01:00
parent ef661a1ea6
commit 8ab06b51bc
10 changed files with 74 additions and 2 deletions

View File

@ -46,6 +46,8 @@ namespace api.Controllers
[HttpPost("login")]
public async Task<IActionResult> Login(UserForLoginDto UserForLoginDto)
{
throw new Exception("lols");
//verifica se o utilizador existe na base de dados e se consegue fazer login
var userFromRepo = await repo.Login(UserForLoginDto.Username.ToLower(), UserForLoginDto.Password);

15
api/Helpers/Extensions.cs Normal file
View File

@ -0,0 +1,15 @@
using Microsoft.AspNetCore.Http;
namespace api.Helpers
{
public static class Extensions
{
public static void AddApplicationError(this HttpResponse response, string message)
{
response.Headers.Add("Application-Error",message);
response.Headers.Add("Access-Control-Expose-Headers","Application-Error");
response.Headers.Add("Access-Control-Allow-Origin","*");
}
}
}

View File

@ -23,7 +23,7 @@
"launchUrl": "api/values",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Production"
}
}
}

View File

@ -1,12 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using api.Data;
using api.Helpers;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@ -65,6 +69,19 @@ namespace api
}
else
{
//globar exception handler
app.UseExceptionHandler(builder => {
builder.Run(async context => {
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
var error= context.Features.Get<IExceptionHandlerFeature>();
if(error!= null){
context.Response.AddApplicationError(error.Error.Message);
await context.Response.WriteAsync(error.Error.Message);
}
});
});
// app.UseHsts();
}

Binary file not shown.

Binary file not shown.

View File

@ -1 +1 @@
c978743fb7bb4bd8de2773d42c543ecf6a79f27c
d7eb3d56420b80ad6f65faaa4a83650bf99d7c85

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,38 @@
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpResponse, HttpRequest, HttpHandler, HttpEvent, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs/observable';
import {_throw} from 'rxjs/observable/throw';
import {catchError} from 'rxjs/operators';
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
catchError(error => {
if (error instanceof HttpErrorResponse) {
if (error.status === 401) {
return _throw(error.statusText);
}
const applicationError = error.headers.get('Application-Error');
if (applicationError) {
return _throw(applicationError);
}
const serverError = error.error;
let modalStateErrors = '';
if (serverError && typeof serverError === 'object'){
for (const key in serverError) {
if (serverError[key]) {
modalStateErrors += serverError[key] + '\n';
}
}
}
return _throw(modalStateErrors || serverError || 'Server Error');
}
})
);
}
}