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()); } }