Added validationError checking in test code.

This commit is contained in:
cshowalter 2022-05-15 22:30:59 -07:00
parent 5736cf1146
commit fdd8667b64
3 changed files with 15 additions and 2 deletions

View File

@ -20,7 +20,12 @@ export class ErrorInterceptor implements HttpInterceptor {
if (error){ if (error){
if (error.status === 400){ if (error.status === 400){
this.toastr.error(error.error.message, error.error.statusCode); if (error.error.errors){
//log('error thrown ' + error.error);
throw error.error;
} else {
this.toastr.error(error.error.message, error.error.statusCode);
}
} }
if (error.status === 401){ if (error.status === 401){

View File

@ -3,4 +3,9 @@
<button (click)="get404Error()" class="btn btn-outline-primary me-3">Test 404 Error</button> <button (click)="get404Error()" class="btn btn-outline-primary me-3">Test 404 Error</button>
<button (click)="get400Error()" class="btn btn-outline-primary me-3">Test 400 Error</button> <button (click)="get400Error()" class="btn btn-outline-primary me-3">Test 400 Error</button>
<button (click)="get400ValidationError()" class="btn btn-outline-primary me-3">Test 400 Validation Error</button> <button (click)="get400ValidationError()" class="btn btn-outline-primary me-3">Test 400 Validation Error</button>
<div class="row mt-5" *ngIf="validationErrors">
<ul class="text-danger">
<li *ngFor="let error of validationErrors">{{error}}</li>
</ul>
</div>
</div> </div>

View File

@ -9,6 +9,7 @@ import { environment } from 'src/environments/environment';
}) })
export class TestErrorComponent implements OnInit { export class TestErrorComponent implements OnInit {
baseURL = environment.apiUrl; baseURL = environment.apiUrl;
validationErrors: any;
constructor(private http: HttpClient) { } constructor(private http: HttpClient) { }
@ -49,7 +50,9 @@ export class TestErrorComponent implements OnInit {
this.http.get(this.baseURL + 'products/five').subscribe( this.http.get(this.baseURL + 'products/five').subscribe(
{ {
next: (response) => { console.log(response); }, next: (response) => { console.log(response); },
error: (e: any) => { console.log(e); }, error: (e: any) => {
//console.log(e.errors);
this.validationErrors = e.errors; },
complete: () => { console.log('complete'); } complete: () => { console.log('complete'); }
} }
); );