Using catchError To Update Component












0















Looking for help catching an HTTP error in an Angular HttpClient get request, and passing that back to the component which originated the request in order to handle the error in a consumer-friendly way.



My Angular 7 app has a reporting module for which I'm trying to add error handling. There is a component report-listwhich uses a changeReport() method in a shared reports service, which updates an observable which the reports-view component subscribes to. When reports-view gets an updated currentReport (which is a report ID number), it fetches the full report from the server with the specified report ID.



Something I came across while mocking up the client side of this module is the possibility that the report ID I try to get will not exist, a condition I should be able to handle. Ideally I'd like to update reports-view with an error saying "Report ID could not be found". I went researching catchError in the Angular docs found only examples of its use in the guide section for RxJS and HttpClient, and in the tutorial for HTTP. Searching observables and pipes didn't help much either. Does anyone know any resources I can read that could help me accomplish this goal, and/or what related concepts I need to read up on and understand in order to implement what I'm trying to do?



reports.service.ts:



export class ReportsService {

private reportsUrl = 'api/reports';

/// Establishes the observable used to subscribe to which report to get
reportSelectionNum: number;
private reportSelection = new BehaviorSubject<number>(this.reportSelectionNum);
currentReport = this.reportSelection.asObservable();

constructor(private http: HttpClient) { }

/// Allows sibling component to update report
changeReport(reportID: number) {
this.reportSelection.next(reportID);
}

getReport(id: number): Observable<Report> {
const url = `${this.reportsUrl}/${id}`;
return this.http.get<Report>(url).pipe(
tap(_ => this.log(`getReport(${id})`)),
catchError(this.handleError<Report>())
);
}


reports-view.component.ts



export class ReportsViewComponent implements OnInit {

report: Report;

reportHeaders: string;
reportData: Array<string>;

constructor(private reportsService: ReportsService) { }

ngOnInit() {
this.reportsService.currentReport.subscribe(report => {
if (report) {
this.getReport(report);
}
});
}

getReport(id: number): void {
this.reportsService.getReport(id).subscribe(report => {
this.report = report;
this.reportHeaders = this.report.data.shift();
this.reportData = this.report.data;
});
}
}


reports-list.component.ts



export class ReportListComponent implements OnInit {

reports: Report;

constructor(private reportsService: ReportsService) { }

ngOnInit() {
this.getReports();
}

getReports(): void {
this.reportsService.getReports().subscribe(reports => this.reports = reports);
}

setReportID(id: number): void {
this.reportsService.changeReport(id);
}

}


My hope is that the report-view component will be notified when an error of a particular type as occurred (such as a 404 error) so I can appropriately update the view content with a consumer-friendly error.










share|improve this question























  • I would look through this guide as well. Good luck!

    – dmcgrandle
    Jan 2 at 23:14
















0















Looking for help catching an HTTP error in an Angular HttpClient get request, and passing that back to the component which originated the request in order to handle the error in a consumer-friendly way.



My Angular 7 app has a reporting module for which I'm trying to add error handling. There is a component report-listwhich uses a changeReport() method in a shared reports service, which updates an observable which the reports-view component subscribes to. When reports-view gets an updated currentReport (which is a report ID number), it fetches the full report from the server with the specified report ID.



Something I came across while mocking up the client side of this module is the possibility that the report ID I try to get will not exist, a condition I should be able to handle. Ideally I'd like to update reports-view with an error saying "Report ID could not be found". I went researching catchError in the Angular docs found only examples of its use in the guide section for RxJS and HttpClient, and in the tutorial for HTTP. Searching observables and pipes didn't help much either. Does anyone know any resources I can read that could help me accomplish this goal, and/or what related concepts I need to read up on and understand in order to implement what I'm trying to do?



reports.service.ts:



export class ReportsService {

private reportsUrl = 'api/reports';

/// Establishes the observable used to subscribe to which report to get
reportSelectionNum: number;
private reportSelection = new BehaviorSubject<number>(this.reportSelectionNum);
currentReport = this.reportSelection.asObservable();

constructor(private http: HttpClient) { }

/// Allows sibling component to update report
changeReport(reportID: number) {
this.reportSelection.next(reportID);
}

getReport(id: number): Observable<Report> {
const url = `${this.reportsUrl}/${id}`;
return this.http.get<Report>(url).pipe(
tap(_ => this.log(`getReport(${id})`)),
catchError(this.handleError<Report>())
);
}


reports-view.component.ts



export class ReportsViewComponent implements OnInit {

report: Report;

reportHeaders: string;
reportData: Array<string>;

constructor(private reportsService: ReportsService) { }

ngOnInit() {
this.reportsService.currentReport.subscribe(report => {
if (report) {
this.getReport(report);
}
});
}

getReport(id: number): void {
this.reportsService.getReport(id).subscribe(report => {
this.report = report;
this.reportHeaders = this.report.data.shift();
this.reportData = this.report.data;
});
}
}


reports-list.component.ts



export class ReportListComponent implements OnInit {

reports: Report;

constructor(private reportsService: ReportsService) { }

ngOnInit() {
this.getReports();
}

getReports(): void {
this.reportsService.getReports().subscribe(reports => this.reports = reports);
}

setReportID(id: number): void {
this.reportsService.changeReport(id);
}

}


My hope is that the report-view component will be notified when an error of a particular type as occurred (such as a 404 error) so I can appropriately update the view content with a consumer-friendly error.










share|improve this question























  • I would look through this guide as well. Good luck!

    – dmcgrandle
    Jan 2 at 23:14














0












0








0








Looking for help catching an HTTP error in an Angular HttpClient get request, and passing that back to the component which originated the request in order to handle the error in a consumer-friendly way.



My Angular 7 app has a reporting module for which I'm trying to add error handling. There is a component report-listwhich uses a changeReport() method in a shared reports service, which updates an observable which the reports-view component subscribes to. When reports-view gets an updated currentReport (which is a report ID number), it fetches the full report from the server with the specified report ID.



Something I came across while mocking up the client side of this module is the possibility that the report ID I try to get will not exist, a condition I should be able to handle. Ideally I'd like to update reports-view with an error saying "Report ID could not be found". I went researching catchError in the Angular docs found only examples of its use in the guide section for RxJS and HttpClient, and in the tutorial for HTTP. Searching observables and pipes didn't help much either. Does anyone know any resources I can read that could help me accomplish this goal, and/or what related concepts I need to read up on and understand in order to implement what I'm trying to do?



reports.service.ts:



export class ReportsService {

private reportsUrl = 'api/reports';

/// Establishes the observable used to subscribe to which report to get
reportSelectionNum: number;
private reportSelection = new BehaviorSubject<number>(this.reportSelectionNum);
currentReport = this.reportSelection.asObservable();

constructor(private http: HttpClient) { }

/// Allows sibling component to update report
changeReport(reportID: number) {
this.reportSelection.next(reportID);
}

getReport(id: number): Observable<Report> {
const url = `${this.reportsUrl}/${id}`;
return this.http.get<Report>(url).pipe(
tap(_ => this.log(`getReport(${id})`)),
catchError(this.handleError<Report>())
);
}


reports-view.component.ts



export class ReportsViewComponent implements OnInit {

report: Report;

reportHeaders: string;
reportData: Array<string>;

constructor(private reportsService: ReportsService) { }

ngOnInit() {
this.reportsService.currentReport.subscribe(report => {
if (report) {
this.getReport(report);
}
});
}

getReport(id: number): void {
this.reportsService.getReport(id).subscribe(report => {
this.report = report;
this.reportHeaders = this.report.data.shift();
this.reportData = this.report.data;
});
}
}


reports-list.component.ts



export class ReportListComponent implements OnInit {

reports: Report;

constructor(private reportsService: ReportsService) { }

ngOnInit() {
this.getReports();
}

getReports(): void {
this.reportsService.getReports().subscribe(reports => this.reports = reports);
}

setReportID(id: number): void {
this.reportsService.changeReport(id);
}

}


My hope is that the report-view component will be notified when an error of a particular type as occurred (such as a 404 error) so I can appropriately update the view content with a consumer-friendly error.










share|improve this question














Looking for help catching an HTTP error in an Angular HttpClient get request, and passing that back to the component which originated the request in order to handle the error in a consumer-friendly way.



My Angular 7 app has a reporting module for which I'm trying to add error handling. There is a component report-listwhich uses a changeReport() method in a shared reports service, which updates an observable which the reports-view component subscribes to. When reports-view gets an updated currentReport (which is a report ID number), it fetches the full report from the server with the specified report ID.



Something I came across while mocking up the client side of this module is the possibility that the report ID I try to get will not exist, a condition I should be able to handle. Ideally I'd like to update reports-view with an error saying "Report ID could not be found". I went researching catchError in the Angular docs found only examples of its use in the guide section for RxJS and HttpClient, and in the tutorial for HTTP. Searching observables and pipes didn't help much either. Does anyone know any resources I can read that could help me accomplish this goal, and/or what related concepts I need to read up on and understand in order to implement what I'm trying to do?



reports.service.ts:



export class ReportsService {

private reportsUrl = 'api/reports';

/// Establishes the observable used to subscribe to which report to get
reportSelectionNum: number;
private reportSelection = new BehaviorSubject<number>(this.reportSelectionNum);
currentReport = this.reportSelection.asObservable();

constructor(private http: HttpClient) { }

/// Allows sibling component to update report
changeReport(reportID: number) {
this.reportSelection.next(reportID);
}

getReport(id: number): Observable<Report> {
const url = `${this.reportsUrl}/${id}`;
return this.http.get<Report>(url).pipe(
tap(_ => this.log(`getReport(${id})`)),
catchError(this.handleError<Report>())
);
}


reports-view.component.ts



export class ReportsViewComponent implements OnInit {

report: Report;

reportHeaders: string;
reportData: Array<string>;

constructor(private reportsService: ReportsService) { }

ngOnInit() {
this.reportsService.currentReport.subscribe(report => {
if (report) {
this.getReport(report);
}
});
}

getReport(id: number): void {
this.reportsService.getReport(id).subscribe(report => {
this.report = report;
this.reportHeaders = this.report.data.shift();
this.reportData = this.report.data;
});
}
}


reports-list.component.ts



export class ReportListComponent implements OnInit {

reports: Report;

constructor(private reportsService: ReportsService) { }

ngOnInit() {
this.getReports();
}

getReports(): void {
this.reportsService.getReports().subscribe(reports => this.reports = reports);
}

setReportID(id: number): void {
this.reportsService.changeReport(id);
}

}


My hope is that the report-view component will be notified when an error of a particular type as occurred (such as a 404 error) so I can appropriately update the view content with a consumer-friendly error.







angular error-handling observable






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 18:09









Thomas ParikkaThomas Parikka

377




377













  • I would look through this guide as well. Good luck!

    – dmcgrandle
    Jan 2 at 23:14



















  • I would look through this guide as well. Good luck!

    – dmcgrandle
    Jan 2 at 23:14

















I would look through this guide as well. Good luck!

– dmcgrandle
Jan 2 at 23:14





I would look through this guide as well. Good luck!

– dmcgrandle
Jan 2 at 23:14












1 Answer
1






active

oldest

votes


















0














I was directed to the RxJS docs, which apparently are were I can find more detail on catchError and observables.






share|improve this answer
























  • Yes, the docs show that you could add parameters to the handleError method. The second parameter could be a dummy Report object that could contain an error message. That Report could be wrapped in an Observable and returned to the caller. Something like catchError(this.handleError<Report>('getReport', dummyReport).

    – rickz
    Jan 3 at 3:34











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011150%2fusing-catcherror-to-update-component%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I was directed to the RxJS docs, which apparently are were I can find more detail on catchError and observables.






share|improve this answer
























  • Yes, the docs show that you could add parameters to the handleError method. The second parameter could be a dummy Report object that could contain an error message. That Report could be wrapped in an Observable and returned to the caller. Something like catchError(this.handleError<Report>('getReport', dummyReport).

    – rickz
    Jan 3 at 3:34
















0














I was directed to the RxJS docs, which apparently are were I can find more detail on catchError and observables.






share|improve this answer
























  • Yes, the docs show that you could add parameters to the handleError method. The second parameter could be a dummy Report object that could contain an error message. That Report could be wrapped in an Observable and returned to the caller. Something like catchError(this.handleError<Report>('getReport', dummyReport).

    – rickz
    Jan 3 at 3:34














0












0








0







I was directed to the RxJS docs, which apparently are were I can find more detail on catchError and observables.






share|improve this answer













I was directed to the RxJS docs, which apparently are were I can find more detail on catchError and observables.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 19:23









Thomas ParikkaThomas Parikka

377




377













  • Yes, the docs show that you could add parameters to the handleError method. The second parameter could be a dummy Report object that could contain an error message. That Report could be wrapped in an Observable and returned to the caller. Something like catchError(this.handleError<Report>('getReport', dummyReport).

    – rickz
    Jan 3 at 3:34



















  • Yes, the docs show that you could add parameters to the handleError method. The second parameter could be a dummy Report object that could contain an error message. That Report could be wrapped in an Observable and returned to the caller. Something like catchError(this.handleError<Report>('getReport', dummyReport).

    – rickz
    Jan 3 at 3:34

















Yes, the docs show that you could add parameters to the handleError method. The second parameter could be a dummy Report object that could contain an error message. That Report could be wrapped in an Observable and returned to the caller. Something like catchError(this.handleError<Report>('getReport', dummyReport).

– rickz
Jan 3 at 3:34





Yes, the docs show that you could add parameters to the handleError method. The second parameter could be a dummy Report object that could contain an error message. That Report could be wrapped in an Observable and returned to the caller. Something like catchError(this.handleError<Report>('getReport', dummyReport).

– rickz
Jan 3 at 3:34




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011150%2fusing-catcherror-to-update-component%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Angular Downloading a file using contenturl with Basic Authentication

Olmecas

Can't read property showImagePicker of undefined in react native iOS