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

public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.lambdaschool.schools.handlers;

import com.lambdaschool.schools.exceptions.ResourceNotFoundException;
import com.lambdaschool.schools.models.ErrorDetail;
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.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.util.Date;

@Order(Ordered.HIGHEST_PRECEDENCE)
@RestControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {

public RestExceptionHandler() { super(); }

@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<ErrorDetail> handleResourceNotFoundException(
ResourceNotFoundException e
) {
ErrorDetail errorDetail = new ErrorDetail();
errorDetail.setTitle("Resource Not Found");
errorDetail.setStatus(HttpStatus.NOT_FOUND.value());
errorDetail.setDetail("Found an error with School: " + e.getMessage());
errorDetail.setTimestamp(new Date());
errorDetail.setDeveloperMessage(e.getClass().getName());
return new ResponseEntity<>(errorDetail, HttpStatus.NOT_FOUND);
}

@Override
protected ResponseEntity<Object> handleExceptionInternal(
Exception e,
Object body,
HttpHeaders headers,
HttpStatus status,
WebRequest request
) {
ErrorDetail errorDetail = new ErrorDetail();
errorDetail.setTitle("Rest Internal Error");
errorDetail.setStatus(status.value());
errorDetail.setDetail("Found an issue with School: " + e.getMessage());
errorDetail.setTimestamp(new Date());
errorDetail.setDeveloperMessage(e.getClass().getName());
return new ResponseEntity<>(errorDetail, status);
}
}
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,4 @@
package com.lambdaschool.schools.models;

public class ValidationError {
}
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
@@ -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.StudCourses;
import com.lambdaschool.schools.models.Student;
Expand All @@ -8,7 +9,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 @@ -49,15 +49,15 @@ public List<Student> findAll()
public Student findStudentById(long id)
{
return studentrepos.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Student id " + id + " not found!"));
.orElseThrow(() -> new ResourceNotFoundException("Student id " + id + " not found!"));
}

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

Expand All @@ -70,7 +70,7 @@ public Student save(Student student)
if (student.getStudentid() != 0)
{
Student oldStudent = studentrepos.findById(student.getStudentid())
.orElseThrow(() -> new EntityNotFoundException("Student id " + student.getStudentid() + " not found!"));
.orElseThrow(() -> new ResourceNotFoundException("Student id " + student.getStudentid() + " not found!"));

newStudent.setStudentid(student.getStudentid());
}
Expand Down
9 changes: 9 additions & 0 deletions schools/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ spring.datasource.initialization-mode=always
# spring.jpa.hibernate.ddl-auto=update
# since we have our data in SeedData, do not also load it from data.sql
# spring.datasource.initialization-mode=never
#
# Used to set the date format for JSON Output
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=America/Los_Angeles
#
# Turns off Spring Boot automatic exception handling
server.error.whitelabel.enabled=false
spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false