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
@@ -0,0 +1,35 @@
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.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> errorDetails = new LinkedHashMap<>();

errorDetails.put("title", errorAttributes.get("error"));
errorDetails.put("status", errorAttributes.get("status"));
errorDetails.put("detail", errorAttributes.get("message"));
errorDetails.put("timestamp", errorAttributes.get("timestamp"));
errorDetails.put("develpoerMessage", "path: " + errorAttributes.get("path"));

errorDetails.put("errors", helperFunctions.getConstraintViolation(this.getError(webRequest)));

return errorDetails;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.lambdaschool.schools.exceptions;

public class ResourceFoundException extends RuntimeException
{
public ResourceFoundException(String message)
{
super("Error From a Lambda App: "+message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.lambdaschool.schools.exceptions;

public class ResourceNotFoundException extends RuntimeException
{
public ResourceNotFoundException(String message)
{
super("Error From a Lambda App: "+message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
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.TypeMismatchException;
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.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;

@Order(Ordered.HIGHEST_PRECEDENCE)
@RestControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler
{
@Autowired
private HelperFunctions helperFunctions;

public RestExceptionHandler()
{
super();
}

@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe)
{
ErrorDetail errorDetail = new ErrorDetail();
errorDetail.setTimestamp(new Date());
errorDetail.setStatus(HttpStatus.NOT_FOUND.value());
errorDetail.setTitle("Resource Not Found");
errorDetail.setDetail(rnfe.getMessage());
errorDetail.setDeveloperMessage(rnfe.getClass().getName());

errorDetail.setErrors(helperFunctions.getConstraintViolation(rnfe));

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

@ExceptionHandler(ResourceFoundException.class)
public ResponseEntity<?> handleResourceFoundException(ResourceFoundException rfe)
{
ErrorDetail errorDetail = new ErrorDetail();
errorDetail.setTimestamp(new Date());
errorDetail.setStatus(HttpStatus.FOUND.value());
errorDetail.setTitle("Resource Found");
errorDetail.setDetail(rfe.getMessage());
errorDetail.setDeveloperMessage(rfe.getClass().getName());

errorDetail.setErrors(helperFunctions.getConstraintViolation(rfe));

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

@Override
protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
{
ErrorDetail errorDetail = new ErrorDetail();
errorDetail.setTimestamp(new Date());
errorDetail.setStatus(status.value());
errorDetail.setTitle("Rest Internal Exception");
errorDetail.setDetail(ex.getMessage());
errorDetail.setDeveloperMessage(ex.getClass().getName());

errorDetail.setErrors(helperFunctions.getConstraintViolation(ex));

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

@Override
protected ResponseEntity<Object> handleTypeMismatch(TypeMismatchException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
{
ErrorDetail errorDetail = new ErrorDetail();
errorDetail.setTimestamp(new Date());
errorDetail.setStatus(status.value());
errorDetail.setTitle("Rest Internal Exception");
errorDetail.setDetail(ex.getMessage());
errorDetail.setDeveloperMessage(ex.getClass().getName());

errorDetail.setErrors(helperFunctions.getConstraintViolation(ex));

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

@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex,
Object body,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
{
ErrorDetail errorDetail = new ErrorDetail();
errorDetail.setTimestamp(new Date());
errorDetail.setStatus(status.value());
errorDetail.setTitle("Rest Internal Exception");
errorDetail.setDetail(ex.getMessage());
errorDetail.setDeveloperMessage(ex.getClass().getName());

errorDetail.setErrors(helperFunctions.getConstraintViolation(ex));

return new ResponseEntity<>(errorDetail, null, status);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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 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,27 @@
package com.lambdaschool.schools.models;

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

public String getCode()
{
return code;
}

public void setCode(String code)
{
this.code = code;
}

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,7 @@
package com.lambdaschool.schools.services;

import com.lambdaschool.schools.exceptions.ResourceFoundException;
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,8 +13,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

Expand Down Expand Up @@ -59,15 +59,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 @@ -80,7 +80,7 @@ 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!"));

// delete the students for the old course we are replacing
for (StudCourses ur : oldCourse.getStudents())
Expand All @@ -96,7 +96,7 @@ public Course save(Course course)
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 @@ -108,7 +108,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.addStudent(newStudent);
Expand All @@ -133,9 +133,9 @@ public void deleteStudentCourse(
long courseid)
{
studentrepos.findById(studentid)
.orElseThrow(() -> new EntityNotFoundException("Student id " + studentid + " not found!"));
.orElseThrow(() -> new ResourceNotFoundException("Student id " + studentid + " not found!"));
courserepos.findById(courseid)
.orElseThrow(() -> new EntityNotFoundException("Course id " + courseid + " not found!"));
.orElseThrow(() -> new ResourceNotFoundException("Course id " + courseid + " not found!"));

if (courserepos.checkStudentCourseCombo(studentid,
courseid)
Expand All @@ -145,7 +145,7 @@ public void deleteStudentCourse(
courseid);
} else
{
throw new EntityNotFoundException("Student and Course Combination Does Not Exists");
throw new ResourceNotFoundException("Student and Course Combination Does Not Exists");
}
}

Expand All @@ -156,9 +156,9 @@ public void addStudCourses(
long courseid)
{
studentrepos.findById(studentid)
.orElseThrow(() -> new EntityNotFoundException("Student id " + studentid + " not found!"));
.orElseThrow(() -> new ResourceNotFoundException("Student id " + studentid + " not found!"));
courserepos.findById(courseid)
.orElseThrow(() -> new EntityNotFoundException("Course id " + courseid + " not found!"));
.orElseThrow(() -> new ResourceNotFoundException("Course id " + courseid + " not found!"));

if (courserepos.checkStudentCourseCombo(studentid,
courseid)
Expand All @@ -169,7 +169,7 @@ public void addStudCourses(
courseid);
} else
{
throw new EntityExistsException("Student and Course Combination Already Exists");
throw new ResourceFoundException("Student and Course Combination Already Exists");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.lambdaschool.schools.services;

import com.lambdaschool.schools.models.ValidationError;

import java.util.List;

public interface HelperFunctions
{
List<ValidationError> getConstraintViolation(Throwable cause);
}
Loading