Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ <h4>update</h4>
<h4>delete</h4>
<pre class="methodSignature">@Transactional
public&nbsp;void&nbsp;delete&#8203;(long&nbsp;id)
throws javax.persistence.EntityNotFoundException</pre>
throws javax.persistence.ResourceNotFoundException</pre>
<div class="block"><span class="descfrmTypeLabel">Description copied from interface:&nbsp;<code><a href="ZooService.html#delete(long)">ZooService</a></code></span></div>
<div class="block">Deletes the course record, it student course combinations, and its telephone items from the database based off of the provided primary key</div>
<dl>
Expand All @@ -385,7 +385,7 @@ <h4>delete</h4>
<dt><span class="paramLabel">Parameters:</span></dt>
<dd><code>id</code> - id The primary key (long) of the course you seek.</dd>
<dt><span class="throwsLabel">Throws:</span></dt>
<dd><code>javax.persistence.EntityNotFoundException</code></dd>
<dd><code>javax.persistence.ResourceNotFoundException</code></dd>
</dl>
</li>
</ul>
Expand Down Expand Up @@ -438,7 +438,7 @@ <h4>saveZooAnimalCombo</h4>
<li class="blockList">
<h4>findZooByLikeName</h4>
<pre class="methodSignature">public&nbsp;java.util.ArrayList&lt;<a href="../models/Zoo.html" title="class in com.lambdaschool.schools.models">Zoo</a>&gt;&nbsp;findZooByLikeName&#8203;(java.lang.String&nbsp;name)
throws javax.persistence.EntityNotFoundException</pre>
throws javax.persistence.ResourceNotFoundException</pre>
<div class="block"><span class="descfrmTypeLabel">Description copied from interface:&nbsp;<code><a href="ZooService.html#findZooByLikeName(java.lang.String)">ZooService</a></code></span></div>
<div class="block">A list of all zoos whose name contains the given substring
A Stretch Goal</div>
Expand All @@ -450,7 +450,7 @@ <h4>findZooByLikeName</h4>
<dt><span class="returnLabel">Returns:</span></dt>
<dd>List of zoos whose name contains the given substring</dd>
<dt><span class="throwsLabel">Throws:</span></dt>
<dd><code>javax.persistence.EntityNotFoundException</code></dd>
<dd><code>javax.persistence.ResourceNotFoundException</code></dd>
</dl>
</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, includeStackTrace);

Map<String, Object> 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.lambdaschool.schools.exceptions;

public class ResourceFoundException extends RuntimeException{
public ResourceFoundException(String message) {
super("Found an issue with School: " + message);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.lambdaschool.schools.handlers;

import com.lambdaschool.schools.exceptions.ResourceFoundException;
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;

public RestExceptionHandler() {
super();
}

@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
ErrorDetail errorDetail = new ErrorDetail();

errorDetail.setTimestamp(new Date());
errorDetail.setTitle("Rest Internal Exception");
errorDetail.setStatus(status.value());
errorDetail.setDetail("Found an error with School: " + ex.getMessage());
errorDetail.setDeveloperMessage(ex.getClass().getName());
errorDetail.setErrors(helperFunctions.getValidationErrors(ex));

return new ResponseEntity<>(errorDetail, null, status);
}


@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe) {
ErrorDetail errorDetail = new ErrorDetail();

errorDetail.setTimestamp(new Date());
errorDetail.setTitle("Resource Not Found");
errorDetail.setStatus(HttpStatus.NOT_FOUND.value());
errorDetail.setDetail(rnfe.getMessage());
errorDetail.setDeveloperMessage(rnfe.getClass().getName());
errorDetail.setErrors(helperFunctions.getValidationErrors(rnfe));

return new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
}

@ExceptionHandler(ResourceFoundException.class)
public ResponseEntity<?> handleResourceFoundException(ResourceNotFoundException rnfe) {
ErrorDetail errorDetail = new ErrorDetail();

errorDetail.setTimestamp(new Date());
errorDetail.setTitle("Unexpected Resource");
errorDetail.setStatus(HttpStatus.BAD_REQUEST.value());
errorDetail.setDetail(rnfe.getMessage());
errorDetail.setDeveloperMessage(rnfe.getClass().getName());
errorDetail.setErrors(helperFunctions.getValidationErrors(rnfe));

return new ResponseEntity<>(errorDetail, null, HttpStatus.BAD_REQUEST);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.lambdaschool.schools.models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ErrorDetail {
private String title;
private int status;
private String detail;
private Date timestamp;
private String developerMessage;
private List<ValidationError> errors = new ArrayList<>();

public ErrorDetail() {
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public String getDetail() {
return detail;
}

public void setDetail(String detail) {
this.detail = detail;
}

public Date getTimestamp() {
return timestamp;
}

public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}

public String getDeveloperMessage() {
return developerMessage;
}

public void setDeveloperMessage(String developerMessage) {
this.developerMessage = developerMessage;
}

public List<ValidationError> getErrors() {
return errors;
}

public void setErrors(List<ValidationError> errors) {
this.errors = errors;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.lambdaschool.schools.models;

public class ValidationError {
private String fieldName;
private String message;

public ValidationError() {
}

public ValidationError(String fieldName, String message) {
this.fieldName = fieldName;
this.message = message;
}

public String getFieldName() {
return fieldName;
}

public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lambdaschool.schools.services;

import com.lambdaschool.schools.exceptions.ResourceNotFoundException;
import com.lambdaschool.schools.models.Course;
import com.lambdaschool.schools.models.Instructor;
import com.lambdaschool.schools.models.StudCourses;
Expand All @@ -11,7 +12,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityNotFoundException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -58,15 +58,15 @@ public List<Course> findAll()
public Course findCourseById(long id)
{
return courserepos.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Course id " + id + " not found!"));
.orElseThrow(() -> new ResourceNotFoundException("Course id " + id + " not found!"));
}

@Transactional
@Override
public void delete(long id)
{
courserepos.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Course id " + id + " not found!"));
.orElseThrow(() -> new ResourceNotFoundException("Course id " + id + " not found!"));
courserepos.deleteById(id);
}

Expand All @@ -79,15 +79,15 @@ public Course save(Course course)
if (course.getCourseid() != 0)
{
Course oldCourse = courserepos.findById(course.getCourseid())
.orElseThrow(() -> new EntityNotFoundException("Course id " + course.getCourseid() + " not found!"));
.orElseThrow(() -> new ResourceNotFoundException("Course id " + course.getCourseid() + " not found!"));

newCourse.setCourseid(course.getCourseid());
}

newCourse.setCoursename(course.getCoursename());
Instructor newInstructor = instructorrepos.findById(course.getInstructor()
.getInstructorid())
.orElseThrow(() -> new EntityNotFoundException("Instructor id " + course.getInstructor()
.orElseThrow(() -> new ResourceNotFoundException("Instructor id " + course.getInstructor()
.getInstructorid() + " not found!"));
newCourse.setInstructor(newInstructor);

Expand All @@ -97,7 +97,7 @@ public Course save(Course course)
{
Student newStudent = studentrepos.findById(sc.getStudent()
.getStudentid())
.orElseThrow(() -> new EntityNotFoundException("Instructor id " + sc.getStudent()
.orElseThrow(() -> new ResourceNotFoundException("Instructor id " + sc.getStudent()
.getStudentid() + " not found!"));

newCourse.getStudents()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.lambdaschool.schools.services;

import com.lambdaschool.schools.models.ValidationError;

import java.util.List;

public interface HelperFunctions {
List<ValidationError> getValidationErrors(Throwable cause);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.lambdaschool.schools.services;

import com.lambdaschool.schools.models.ValidationError;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.stereotype.Service;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;

import java.util.ArrayList;
import java.util.List;

@Service(value = "helperFunctions")
public class HelperFunctionsImpl implements HelperFunctions{
@Override
public List<ValidationError> getValidationErrors(Throwable cause) {
List<ValidationError> validationErrorList = new ArrayList<>();

while (cause != null && !(cause instanceof ConstraintViolationException || cause instanceof MethodArgumentNotValidException)) {
cause = cause.getCause();
}

if(cause != null ) {
if(cause instanceof ConstraintViolationException) {
ConstraintViolationException ex = (ConstraintViolationException) cause;
System.out.printf("ConstraintViolationException!!!!!!!!!!!" );
ValidationError newVE = new ValidationError();
newVE.setMessage(ex.getConstraintName());
newVE.setFieldName(ex.getMessage());

validationErrorList.add(newVE);
} else {
MethodArgumentNotValidException ex = (MethodArgumentNotValidException) cause;
System.out.printf("MethodArgumentNotValidException!!!!!!!!!!!" );
List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
for(FieldError fe : fieldErrors) {
ValidationError newVE = new ValidationError();
newVE.setMessage(fe.getDefaultMessage());
newVE.setFieldName(fe.getField());

validationErrorList.add(newVE);
}
}
}

return validationErrorList;
}
}
Loading