From 06defa5bb1bf199f22576e200c4363a583b870a4 Mon Sep 17 00:00:00 2001
From: eet349 <51262590+eet349@users.noreply.github.com>
Date: Mon, 19 Apr 2021 19:42:10 -0500
Subject: [PATCH 1/2] Completes MVP.
---
.../zoos/services/ZooServiceImpl.html | 8 +--
.../exceptions/CustomErrorDetails.java | 34 +++++++++
.../exceptions/ResourceNotFoundException.java | 8 +++
.../handlers/RestExceptionHandler.java | 70 +++++++++++++++++++
.../schools/models/ErrorDetail.java | 65 +++++++++++++++++
.../schools/models/ValidationError.java | 30 ++++++++
.../schools/services/CoursesServiceImpl.java | 12 ++--
.../schools/services/HelperFunctions.java | 9 +++
.../schools/services/HelperFunctionsImpl.java | 47 +++++++++++++
.../schools/services/StudentServiceImpl.java | 8 +--
.../src/main/resources/application.properties | 6 ++
11 files changed, 283 insertions(+), 14 deletions(-)
create mode 100644 schools/src/main/java/com/lambdaschool/schools/exceptions/CustomErrorDetails.java
create mode 100644 schools/src/main/java/com/lambdaschool/schools/exceptions/ResourceNotFoundException.java
create mode 100644 schools/src/main/java/com/lambdaschool/schools/handlers/RestExceptionHandler.java
create mode 100644 schools/src/main/java/com/lambdaschool/schools/models/ErrorDetail.java
create mode 100644 schools/src/main/java/com/lambdaschool/schools/models/ValidationError.java
create mode 100644 schools/src/main/java/com/lambdaschool/schools/services/HelperFunctions.java
create mode 100644 schools/src/main/java/com/lambdaschool/schools/services/HelperFunctionsImpl.java
diff --git a/schools/javadocs/com/lambdaschool/zoos/services/ZooServiceImpl.html b/schools/javadocs/com/lambdaschool/zoos/services/ZooServiceImpl.html
index 10bef294..845a2d71 100644
--- a/schools/javadocs/com/lambdaschool/zoos/services/ZooServiceImpl.html
+++ b/schools/javadocs/com/lambdaschool/zoos/services/ZooServiceImpl.html
@@ -376,7 +376,7 @@
update
delete
@Transactional
public void delete(long id)
- throws javax.persistence.EntityNotFoundException
+ throws javax.persistence.ResourceNotFoundException
Deletes the course record, it student course combinations, and its telephone items from the database based off of the provided primary key
@@ -385,7 +385,7 @@ delete
- Parameters:
id - id The primary key (long) of the course you seek.
- Throws:
-javax.persistence.EntityNotFoundException
+javax.persistence.ResourceNotFoundException
@@ -438,7 +438,7 @@ saveZooAnimalCombo
findZooByLikeName
public java.util.ArrayList<Zoo> findZooByLikeName(java.lang.String name)
- throws javax.persistence.EntityNotFoundException
+ throws javax.persistence.ResourceNotFoundException
A list of all zoos whose name contains the given substring
A Stretch Goal
@@ -450,7 +450,7 @@ findZooByLikeName
Returns:
List of zoos whose name contains the given substring
Throws:
-javax.persistence.EntityNotFoundException
+javax.persistence.ResourceNotFoundException
diff --git a/schools/src/main/java/com/lambdaschool/schools/exceptions/CustomErrorDetails.java b/schools/src/main/java/com/lambdaschool/schools/exceptions/CustomErrorDetails.java
new file mode 100644
index 00000000..d363ce08
--- /dev/null
+++ b/schools/src/main/java/com/lambdaschool/schools/exceptions/CustomErrorDetails.java
@@ -0,0 +1,34 @@
+package com.lambdaschool.schools.exceptions;
+
+import com.lambdaschool.schools.services.HelperFunctions;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
+import org.springframework.stereotype.Component;
+import org.springframework.web.context.request.WebRequest;
+
+import java.util.Date;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+@Component
+public class CustomErrorDetails extends DefaultErrorAttributes {
+ @Autowired
+ private HelperFunctions helperFunctions;
+
+ @Override
+ public Map getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
+ Map errorAttributes = super.getErrorAttributes(webRequest, includeStackTrace);
+
+ Map rtnAttributes = new LinkedHashMap<>();
+
+ rtnAttributes.put("title", errorAttributes.get("error"));
+ rtnAttributes.put("status", errorAttributes.get("status"));
+ rtnAttributes.put("detail", errorAttributes.get("message"));
+ rtnAttributes.put("timestamp", new Date());
+ rtnAttributes.put("developerMessage", "path: " + errorAttributes.get("path"));
+
+ rtnAttributes.put("errors", helperFunctions.getValidationErrors(this.getError(webRequest)));
+
+ return rtnAttributes;
+ }
+}
diff --git a/schools/src/main/java/com/lambdaschool/schools/exceptions/ResourceNotFoundException.java b/schools/src/main/java/com/lambdaschool/schools/exceptions/ResourceNotFoundException.java
new file mode 100644
index 00000000..6d59442f
--- /dev/null
+++ b/schools/src/main/java/com/lambdaschool/schools/exceptions/ResourceNotFoundException.java
@@ -0,0 +1,8 @@
+package com.lambdaschool.schools.exceptions;
+
+
+public class ResourceNotFoundException extends RuntimeException{
+ public ResourceNotFoundException(String message) {
+ super("Found an error with School: " + message);
+ }
+}
diff --git a/schools/src/main/java/com/lambdaschool/schools/handlers/RestExceptionHandler.java b/schools/src/main/java/com/lambdaschool/schools/handlers/RestExceptionHandler.java
new file mode 100644
index 00000000..34abdc56
--- /dev/null
+++ b/schools/src/main/java/com/lambdaschool/schools/handlers/RestExceptionHandler.java
@@ -0,0 +1,70 @@
+package com.lambdaschool.schools.handlers;
+
+import com.lambdaschool.schools.exceptions.ResourceNotFoundException;
+import com.lambdaschool.schools.models.ErrorDetail;
+import com.lambdaschool.schools.services.HelperFunctions;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.Ordered;
+import org.springframework.core.annotation.Order;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.MissingPathVariableException;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+import org.springframework.web.context.request.WebRequest;
+import org.springframework.web.servlet.NoHandlerFoundException;
+import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
+
+import java.util.Date;
+
+@RestControllerAdvice
+@Order(Ordered.HIGHEST_PRECEDENCE)
+public class RestExceptionHandler extends ResponseEntityExceptionHandler {
+ @Autowired
+ private HelperFunctions helperFunctions;
+
+ @Override
+ protected ResponseEntity