11# Error Handling
22
3- - [ API Errors] ( #api-errors )
4- - [ Validation Errors] ( #validation-errors )
5-
63## API Errors
74
85To handle API response errors, multiple exceptions are provided. You can see all available in the following example:
@@ -15,13 +12,9 @@ use ProgrammatorDev\OpenWeatherMap\Exception\UnauthorizedException;
1512use ProgrammatorDev\OpenWeatherMap\Exception\UnexpectedErrorException;
1613
1714try {
18- $location = $api->geocoding()->getByZipCode('1000-001', 'pt');
19- $coordinate = $location->getCoordinate();
15+ // ...
2016
21- $weather = $api->oneCall()->getWeather(
22- $coordinate->getLatitude(),
23- $coordinate->getLongitude()
24- );
17+ $weather = $api->oneCall()->getWeather($latitude, $longitude);
2518}
2619// bad request to the API
2720catch (BadRequestException $exception) {
@@ -33,7 +26,7 @@ catch (UnauthorizedException $exception) {
3326 echo $exception->getCode(); // 401
3427 echo $exception->getMessage();
3528}
36- // resource not found
29+ // resource not found,
3730// for example, when trying to get a location with a zip code that does not exist
3831catch (NotFoundException $exception) {
3932 echo $exception->getCode(); // 404
@@ -57,35 +50,13 @@ To catch all API errors with a single exception, `ApiErrorException` is availabl
5750use ProgrammatorDev\OpenWeatherMap\Exception\ApiErrorException;
5851
5952try {
60- $location = $api->geocoding()->getByZipCode('1000-001', 'pt');
61- $coordinate = $location->getCoordinate();
62-
63- $weather = $api->oneCall()->getWeather(
64- $coordinate->getLatitude(),
65- $coordinate->getLongitude()
66- );
53+ // ...
54+
55+ $weather = $api->oneCall()->getWeather($latitude, $longitude);
6756}
6857// catches all API response errors
6958catch (ApiErrorException $exception) {
7059 echo $exception->getCode();
7160 echo $exception->getMessage();
7261}
73- ```
74-
75- ## Validation Errors
76-
77- To catch invalid input data (like an out of range coordinate, blank location name, etc.), the ` ValidationException ` is available:
78-
79- ``` php
80- use ProgrammatorDev\Validator\Exception\ValidationException;
81-
82- try {
83- // an invalid latitude value is given
84- $weather = $api->weather()->getCurrent(999, 50);
85- }
86- catch (ValidationException $exception) {
87- // should print:
88- // The latitude value should be between -90 and 90, 999 given.
89- echo $exception->getMessage();
90- }
9162```
0 commit comments