From 40b0426129ac64b866d9e38980cbf5b48084de61 Mon Sep 17 00:00:00 2001 From: anhefti Date: Wed, 17 Mar 2021 09:08:17 +0100 Subject: [PATCH] better logging for moodle access --- .../moodle/MoodleRestTemplateFactory.java | 55 +++++++++++++------ 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleRestTemplateFactory.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleRestTemplateFactory.java index 605bc37b..ca878b2f 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleRestTemplateFactory.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleRestTemplateFactory.java @@ -336,27 +336,48 @@ class MoodleRestTemplateFactory { private void requestAccessToken() { - final ResponseEntity response = super.exchange( - this.serverURL + this.tokenPath, - HttpMethod.GET, - this.tokenReqEntity, - String.class, - this.tokenReqURIVars); - - if (response.getStatusCode() != HttpStatus.OK) { - throw new RuntimeException("Failed to gain access token for LMS (Moodle): lmsSetup: " + - MoodleRestTemplateFactory.this.lmsSetup + " response: " + response.getBody()); - } - try { - final MoodleToken moodleToken = MoodleRestTemplateFactory.this.jsonMapper.readValue( - response.getBody(), - MoodleToken.class); + final ResponseEntity response = super.exchange( + this.serverURL + this.tokenPath, + HttpMethod.GET, + this.tokenReqEntity, + String.class, + this.tokenReqURIVars); + + if (response.getStatusCode() != HttpStatus.OK) { + log.error("Failed to gain access token for LMS (Moodle): lmsSetup: {} response: {} : {}", + MoodleRestTemplateFactory.this.lmsSetup, + response.getStatusCode(), + response.getBody()); + throw new RuntimeException("Failed to gain access token for LMS (Moodle): lmsSetup: " + + MoodleRestTemplateFactory.this.lmsSetup + " response: " + response.getBody()); + } + + try { + final MoodleToken moodleToken = MoodleRestTemplateFactory.this.jsonMapper.readValue( + response.getBody(), + MoodleToken.class); + + if (moodleToken == null || moodleToken.token == null) { + throw new RuntimeException("Access Token request with 200 but no or invalid token body"); + } + + this.accessToken = moodleToken.token; + } catch (final Exception e) { + log.error("Failed to gain access token for LMS (Moodle): lmsSetup: {} response: {} : {}", + MoodleRestTemplateFactory.this.lmsSetup, + response.getStatusCode(), + response.getBody()); + throw new RuntimeException("Failed to gain access token for LMS (Moodle): lmsSetup: " + + MoodleRestTemplateFactory.this.lmsSetup + " response: " + response.getBody(), e); + } - this.accessToken = moodleToken.token; } catch (final Exception e) { + log.error("Failed to gain access token for LMS (Moodle): lmsSetup: {} :", + MoodleRestTemplateFactory.this.lmsSetup, + e); throw new RuntimeException("Failed to gain access token for LMS (Moodle): lmsSetup: " + - MoodleRestTemplateFactory.this.lmsSetup + " response: " + response.getBody(), e); + MoodleRestTemplateFactory.this.lmsSetup + " cause: " + e.getMessage()); } }