updates
This commit is contained in:
parent
ef661a1ea6
commit
8ab06b51bc
|
@ -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);
|
||||
|
||||
|
|
|
@ -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","*");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -23,7 +23,7 @@
|
|||
"launchUrl": "api/values",
|
||||
"applicationUrl": "https://localhost:5001;http://localhost:5000",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
"ASPNETCORE_ENVIRONMENT": "Production"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.
|
@ -1 +1 @@
|
|||
c978743fb7bb4bd8de2773d42c543ecf6a79f27c
|
||||
d7eb3d56420b80ad6f65faaa4a83650bf99d7c85
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -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');
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue