вывод сообщения об ошибке на экран HttpClient
Работаю с api openweathermap. Как вывести сообщение об ошибке на экран и в консоль если неверно указан город? Заранее благодарю за ответ
import { Injectable} from '@angular/core';
import { HttpClient, HttpParams} from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class WeatherService {
url = 'http://api.openweathermap.org/data/2.5/weather';
apiKey = 'cd568793ed4c1a5791ef68c832393f50';
constructor(private http: HttpClient) { }
getWeatherDataByCityName(city: string){
let params = new HttpParams()
.set('q', city)
.set('units', 'imperial')
.set('appid', this.apiKey)
return this.http.get(this.url, {params});
}
}
import { Component, OnInit} from '@angular/core';
import { WeatherService} from '../weather.service';
@Component({
selector: 'app-forecast',
templateUrl: './forecast.component.html',
styleUrls: ['./forecast.component.css']
})
export class ForecastComponent implements OnInit {
weather;
constructor(private weatherService: WeatherService) {
}
ngOnInit(): void {
}
getCity(city){
this.weatherService.getWeatherDataByCityName(city).subscribe(data =>{
this.weather = data;
})
}
}