From ed53d81dc7a13097292321105ebc43c5aac12d57 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Tue, 4 Apr 2023 12:29:50 +0200 Subject: [PATCH] MDL-77842 badges: Improve debugging information for Badgr In MDL-77086 some extra information was added when a badge can't be sent to a backpack. However, as the Badgr is not following the specification, it's still hard to debug it when it fails (as raised in MDL-75552, Badgr is not working because, in some cases, it's returning the "Method Not Allowed / Request method 'POST' not supported" error. --- badges/classes/backpack_api2p1.php | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/badges/classes/backpack_api2p1.php b/badges/classes/backpack_api2p1.php index d00521a37fc..1d90b420759 100644 --- a/badges/classes/backpack_api2p1.php +++ b/badges/classes/backpack_api2p1.php @@ -228,11 +228,26 @@ class backpack_api2p1 { $msg['status'] = \core\output\notification::NOTIFY_SUCCESS; $msg['message'] = get_string('addedtobackpack', 'badges'); } else { - $statuserror = $response->status->error; - if (is_array($statuserror)) { + if ($response) { // Although the specification defines that status error is a string, some providers, like Badgr, are wrongly - // returning an array. It has been reported, but adding this extra check doesn't hurt, just in case. - $statuserror = implode($statuserror); + // returning an array. It has been reported, but adding these extra checks doesn't hurt, just in case. + if ( + property_exists($response, 'status') && + is_object($response->status) && + property_exists($response->status, 'error') + ) { + $statuserror = $response->status->error; + if (is_array($statuserror)) { + $statuserror = implode($statuserror); + } + } else if (property_exists($response, 'error')) { + $statuserror = $response->error; + if (property_exists($response, 'message')) { + $statuserror .= '. Message: ' . $response->message; + } + } + } else { + $statuserror = 'Empty response'; } $data = [ 'badgename' => $data['assertion']['badge']['name'],