Rule Definition
In general, silently passing errors is a bad practice and can result in unexpected behavior and render more difficult finding the origin of bugs. Because of the asynchronous nature of observables returned by HttpClient service calls, arising errors from observables need to be handled by user-provided error callbacks instead of try/catch blocks.
Remediation
Ensure that you provide an error callback to the subscribe method.
Violation Code Sample
export class AppComponent {
constructor(private http: HttpClient) {
}
getData() {
this.http.get(this.url).subscribe( // subscription to Observable
(data: Config) => this.config = { ...data }
);
// ...
}
}
Fixed Code Sample
export class AppComponent {
constructor(private http: HttpClient) {
}
getData() {
this.http.get(this.url).subscribe( // subscription to Observable
(data: Config) => this.config = { ...data },
error => this.error = error // error path
);
// ...
}
}
Reference
https://angular.io/guide/http#error-handling
https://angular.io/guide/observables
Related Technologies
Technical Criterion
CWE-391 - Unchecked Error Condition
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.