@@ -119,10 +119,10 @@ public function __construct($apiKey = '', $fetcher = null, $cache = false, $seco
119119 }
120120
121121 if ($ cache !== false && !($ cache instanceof AbstractCache)) {
122- throw new \Exception ('The cache class must implement the FetcherInterface! ' );
122+ throw new \InvalidArgumentException ('The cache class must implement the FetcherInterface! ' );
123123 }
124124 if (!is_numeric ($ seconds )) {
125- throw new \Exception ('$seconds must be numeric. ' );
125+ throw new \InvalidArgumentException ('$seconds must be numeric. ' );
126126 }
127127 if (!isset ($ fetcher )) {
128128 $ fetcher = (function_exists ('curl_version ' )) ? new CurlFetcher () : new FileGetContentsFetcher ();
@@ -546,7 +546,7 @@ private function parseXML($answer)
546546 // OpenWeatherMap always uses json for errors, even if one specifies xml as format.
547547 $ error = json_decode ($ answer , true );
548548 if (isset ($ error ['message ' ])) {
549- throw new OWMException ($ error ['message ' ], $ error ['cod ' ]);
549+ throw new OWMException ($ error ['message ' ], isset ( $ error ['cod ' ]) ? $ error [ ' cod ' ] : 0 );
550550 } else {
551551 throw new OWMException ('Unknown fatal error: OpenWeatherMap returned the following json object: ' . $ answer );
552552 }
@@ -563,9 +563,28 @@ private function parseJson($answer)
563563 {
564564 $ json = json_decode ($ answer );
565565 if (json_last_error () !== JSON_ERROR_NONE ) {
566- throw new OWMException ('OpenWeatherMap returned an invalid json object: ' . json_last_error_msg ());
566+ throw new OWMException ('OpenWeatherMap returned an invalid json object. JSON error was : ' . $ this -> json_last_error_msg ());
567567 }
568568
569569 return $ json ;
570570 }
571+
572+ private function json_last_error_msg ()
573+ {
574+ if (function_exists ('json_last_error_msg ' )) {
575+ return json_last_error_msg ();
576+ }
577+
578+ static $ ERRORS = array (
579+ JSON_ERROR_NONE => 'No error ' ,
580+ JSON_ERROR_DEPTH => 'Maximum stack depth exceeded ' ,
581+ JSON_ERROR_STATE_MISMATCH => 'State mismatch (invalid or malformed JSON) ' ,
582+ JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded ' ,
583+ JSON_ERROR_SYNTAX => 'Syntax error ' ,
584+ JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded '
585+ );
586+
587+ $ error = json_last_error ();
588+ return isset ($ ERRORS [$ error ]) ? $ ERRORS [$ error ] : 'Unknown error ' ;
589+ }
571590}
0 commit comments