Skip to content

Commit

Permalink
IllegalArgumentException ISBNValidatorNotAccessibleAdvice
Browse files Browse the repository at this point in the history
  • Loading branch information
DerKarums committed Oct 13, 2021
1 parent 77b4baa commit 3bb77f0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package de.hswhameln.isbnvalidator.exceptions;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class ISBNValidatorNotAccessibleAdvice {

@ResponseBody
@ExceptionHandler(ISBNValidatorNotAccessibleException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
String isbnValidatorNotAccessibleHandler(ISBNValidatorNotAccessibleException ex) {
return ex.getMessage();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package de.hswhameln.isbnvalidator.exceptions;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class IllegalArgumentAdvice {

@ResponseBody
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
String illegalArgumentHandler(IllegalArgumentException ex) {
return ex.getMessage();
}
}
12 changes: 8 additions & 4 deletions src/main/java/de/hswhameln/isbnvalidator/utils/RestConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ public RestConsumer(String url) {
public JSONObject postRequest(String urlPath, Map<String, String> params) {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(url + urlPath, params, String.class);
if(response.getStatusCode().equals(HttpStatus.OK)) {
if (response.getStatusCode().equals(HttpStatus.OK)) {
try {
return new JSONObject(response.getBody());
} catch(JSONException e) {
} catch (JSONException e) {
throw new ISBNValidatorNotAccessibleException();
}
} else if (response.getStatusCode().equals(HttpStatus.CONFLICT)) {
throw new IllegalArgumentException();
} else {
throw new ISBNValidatorNotAccessibleException();
}
Expand All @@ -36,12 +38,14 @@ public JSONObject getRequest(String urlPath, String isbn) {
params.put("isbn", isbn);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.getForEntity(url + urlPath, String.class, params);
if(response.getStatusCode().equals(HttpStatus.OK)) {
if (response.getStatusCode().equals(HttpStatus.OK)) {
try {
return new JSONObject(response.getBody());
} catch(JSONException e) {
} catch (JSONException e) {
throw new ISBNValidatorNotAccessibleException();
}
} else if (response.getStatusCode().equals(HttpStatus.CONFLICT)) {
throw new IllegalArgumentException();
} else {
throw new ISBNValidatorNotAccessibleException();
}
Expand Down

0 comments on commit 3bb77f0

Please sign in to comment.