Skip to content
Open

mvp #15

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.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
* The application turns off any automatic web page generate done by Spring. This is done to improve exception handling.
* However, we do need some web page generate done for Swagger, so we do that here.
*/
@Configuration
public class SwaggerWebMVC
implements WebMvcConfigurer
{
/**
* Adds the Swagger web pages to Spring.
* This still gives the following warning
* <p>
* No mapping for GET /
* No mapping for GET /csrf
* <p>
* All works though
*
* @param registry the place that holds the web pages for Spring
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");

registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Component
public class CustomErrorDetails extends DefaultErrorAttributes
{
@Autowired
HelperFunctions helperFunctions;

@Override
public Map<String, Object> getErrorAttributes(
WebRequest webRequest,
boolean includeStackTrace)
{
// title
// status
// detail
// timestamp
// developermessage
//
// errors

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

public class ResourceNotFoundException extends RuntimeException {

public ResourceNotFoundException(String message) {
super("Error from a lambda school app: " + message);
}
}
Loading