public static function process_json_response()

in classes/utils.php [41:80]


    public static function process_json_response($response, array $expectedstructure = array()) {
        $backtrace = debug_backtrace(0);
        $callingclass = (isset($backtrace[1]['class'])) ? $backtrace[1]['class'] : '?';
        $callingfunc = (isset($backtrace[1]['function'])) ? $backtrace[1]['function'] : '?';
        $callingline = (isset($backtrace[0]['line'])) ? $backtrace[0]['line'] : '?';
        $caller = $callingclass.'::'.$callingfunc.':'.$callingline;

        $result = @json_decode($response, true);
        if (empty($result) || !is_array($result)) {
            self::debug('Bad response received', $caller, $response);
            throw new \moodle_exception('erroroidccall', 'auth_oidc');
        }

        if (isset($result['error'])) {
            $errmsg = 'Error response received.';
            self::debug($errmsg, $caller, $result);
            if (isset($result['error_description'])) {
                throw new \moodle_exception('erroroidccall_message', 'auth_oidc', '', $result['error_description']);
            } else {
                throw new \moodle_exception('erroroidccall', 'auth_oidc');
            }
        }

        foreach ($expectedstructure as $key => $val) {
            if (!isset($result[$key])) {
                $errmsg = 'Invalid structure received. No "'.$key.'"';
                self::debug($errmsg, $caller, $result);
                throw new \moodle_exception('erroroidccall', 'auth_oidc');
            }

            if ($val !== null && $result[$key] !== $val) {
                $strreceivedval = self::tostring($result[$key]);
                $strval = self::tostring($val);
                $errmsg = 'Invalid structure received. Invalid "'.$key.'". Received "'.$strreceivedval.'", expected "'.$strval.'"';
                self::debug($errmsg, $caller, $result);
                throw new \moodle_exception('erroroidccall', 'auth_oidc');
            }
        }
        return $result;
    }