Skip to content

Commit ce558d0

Browse files
committed
Update error message handling
1 parent abd4117 commit ce558d0

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

frontend/src/app/questions/question.service.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
1+
import { HttpClient, HttpErrorResponse, HttpHeaders, HttpParams } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
33
import { API_CONFIG } from '../api.config';
4-
import { Observable } from 'rxjs';
4+
import { catchError, Observable, throwError } from 'rxjs';
55
import { SingleQuestionResponse, QuestionResponse, QuestionBody } from './question.model';
66
import { TopicResponse } from './topic.model';
77

@@ -64,14 +64,18 @@ export class QuestionService {
6464
}
6565

6666
addQuestion(question: QuestionBody): Observable<SingleQuestionResponse> {
67-
return this.http.post<SingleQuestionResponse>(this.baseUrl + '/questions', question, this.httpOptions);
67+
return this.http.post<SingleQuestionResponse>(this.baseUrl + '/questions', question, this.httpOptions).pipe(catchError(this.handleError));
6868
}
6969

7070
updateQuestion(id: number, question: QuestionBody): Observable<SingleQuestionResponse> {
71-
return this.http.put<SingleQuestionResponse>(this.baseUrl + '/questions/' + id, question, this.httpOptions);
71+
return this.http.put<SingleQuestionResponse>(this.baseUrl + '/questions/' + id, question, this.httpOptions).pipe(catchError(this.handleError));
7272
}
7373

7474
deleteQuestion(id: number): Observable<SingleQuestionResponse> {
75-
return this.http.delete<SingleQuestionResponse>(this.baseUrl + '/questions/' + id);
75+
return this.http.delete<SingleQuestionResponse>(this.baseUrl + '/questions/' + id).pipe(catchError(this.handleError));
76+
}
77+
78+
handleError(error: HttpErrorResponse) {
79+
return throwError(error);
7680
}
7781
}

frontend/src/app/questions/questions.component.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { Difficulty } from './difficulty.model';
1818
import { DifficultyLevels } from './difficulty-levels.enum';
1919
import { QuestionService } from './question.service';
2020
import { forkJoin } from 'rxjs';
21+
import { HttpErrorResponse } from '@angular/common/http';
2122

2223
@Component({
2324
selector: 'app-questions',
@@ -201,12 +202,12 @@ export class QuestionsComponent implements OnInit {
201202
this.questions = [...this.questions, response.data];
202203
}
203204
},
204-
error: (error: Error) => {
205+
error: (error: HttpErrorResponse) => {
205206
console.log(error);
206207
this.messageService.add({
207208
severity: 'error',
208209
summary: 'Error',
209-
detail: 'Failed to add new question. Please try again later.',
210+
detail: 'Failed to add new question. ' + error.error.message,
210211
life: 3000,
211212
});
212213
},
@@ -230,12 +231,12 @@ export class QuestionsComponent implements OnInit {
230231
this.questions = this.questions?.filter(val => !this.selectedQuestions?.includes(val));
231232
this.selectedQuestions = null;
232233
},
233-
error: () => {
234+
error: (error: HttpErrorResponse) => {
234235
// Handle any errors from the forkJoin if necessary
235236
this.messageService.add({
236237
severity: 'error',
237238
summary: 'Error',
238-
detail: 'Some questions could not be deleted. Please try again later.',
239+
detail: 'Some questions could not be deleted. ' + error.error.message,
239240
life: 3000,
240241
});
241242
},
@@ -255,13 +256,13 @@ export class QuestionsComponent implements OnInit {
255256
next: (response: SingleQuestionResponse) => {
256257
this.questions[this.questions.findIndex(x => x.id == id)] = response.data;
257258
},
258-
error: (error: Error) => {
259+
error: (error: HttpErrorResponse) => {
259260
console.log(error);
260261
console.log(question);
261262
this.messageService.add({
262263
severity: 'error',
263264
summary: 'Error',
264-
detail: 'Failed to edit question. Please try again later.',
265+
detail: error.error.message,
265266
life: 3000,
266267
});
267268
},

0 commit comments

Comments
 (0)