116 lines
4.5 KiB
Java
116 lines
4.5 KiB
Java
package com.iconplus.smartproc.exception;
|
|
|
|
import lombok.extern.log4j.Log4j2;
|
|
import org.apache.velocity.Template;
|
|
import org.apache.velocity.VelocityContext;
|
|
import org.apache.velocity.app.Velocity;
|
|
import org.apache.velocity.app.VelocityEngine;
|
|
import org.apache.velocity.context.Context;
|
|
import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
|
|
import org.apache.velocity.runtime.resource.util.StringResourceRepository;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
import java.io.StringWriter;
|
|
import java.util.Map;
|
|
|
|
import static org.apache.velocity.runtime.RuntimeConstants.RESOURCE_LOADER;
|
|
|
|
@Configuration
|
|
@Log4j2
|
|
public class ErrorHelper {
|
|
|
|
public static final String RAW_TEMPLATE = "rawTemplate";
|
|
private final VelocityEngine engine;
|
|
|
|
public ErrorHelper() {
|
|
engine = new VelocityEngine();
|
|
this.addVelocityProperties();
|
|
engine.init();
|
|
}
|
|
|
|
public ResponseEntity<ErrorResponse> throwErrorException(String errorCode, HttpStatus httpStatus) {
|
|
|
|
ErrorResponse errorResponse = new ErrorResponse();
|
|
errorResponse.setCode(errorCode);
|
|
errorResponse.setTitle("Kesalahan terjadi");
|
|
errorResponse.setMessage("Kesalahan terjadi");
|
|
|
|
return new ResponseEntity<>(errorResponse, new HttpHeaders(), httpStatus);
|
|
}
|
|
|
|
public ResponseEntity<ErrorResponse> throwErrorExceptionWithHardcodedMsg(String errorCode, HttpStatus httpStatus,
|
|
String errorDesc, String errorMsg) {
|
|
|
|
ErrorResponse errorResponse = new ErrorResponse();
|
|
errorResponse.setCode(errorCode);
|
|
errorResponse.setTitle(errorDesc);
|
|
errorResponse.setMessage(errorMsg);
|
|
return new ResponseEntity<>(errorResponse, new HttpHeaders(), httpStatus);
|
|
}
|
|
|
|
public ResponseEntity<ErrorResponse> throwErrorExceptionWithMetadata(String errorCode, HttpStatus httpStatus,
|
|
Map<String, String> metadata) {
|
|
|
|
ErrorResponse errorResponse = new ErrorResponse();
|
|
errorResponse.setCode(errorCode);
|
|
errorResponse.setTitle("Kesalahan terjadi");
|
|
errorResponse.setMessage(transform("Kesalahan terjadi", metadata));
|
|
|
|
return new ResponseEntity<>(errorResponse, new HttpHeaders(), httpStatus);
|
|
}
|
|
|
|
public ResponseEntity<ErrorResponse> throwErrorExceptionWithMessageAndMetadataWithoutLocalization(HttpStatus httpStatus, String errorCode, String errorTitle, String errorMessage,
|
|
Map<String, String> metadata) {
|
|
|
|
ErrorResponse errorResponse = new ErrorResponse();
|
|
errorResponse.setCode(errorCode);
|
|
errorResponse.setTitle(errorTitle);
|
|
errorResponse.setMessage(transform(errorMessage, metadata));
|
|
|
|
return new ResponseEntity<>(errorResponse, new HttpHeaders(), httpStatus);
|
|
}
|
|
|
|
private String transform(String rawNotificationTemplate, Map<String, String> parameters) {
|
|
|
|
VelocityContext context = this.getVelocityContext(rawNotificationTemplate, parameters);
|
|
Template template = engine.getTemplate(RAW_TEMPLATE);
|
|
|
|
StringWriter templateWriter = new StringWriter(rawNotificationTemplate.length());
|
|
template.merge(context, templateWriter);
|
|
|
|
return templateWriter.toString();
|
|
}
|
|
|
|
private VelocityContext getVelocityContext(String rawNotificationTemplate, Map<String, String> parameters) {
|
|
StringResourceRepository resourceRepository = (StringResourceRepository) engine
|
|
.getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
|
|
resourceRepository.putStringResource(RAW_TEMPLATE, rawNotificationTemplate);
|
|
|
|
return new VelocityContext((Context) parameters);
|
|
}
|
|
|
|
private void addVelocityProperties() {
|
|
Velocity.addProperty("string.resource.loader.description", "Velocity StringResource loader");
|
|
|
|
engine.addProperty(RESOURCE_LOADER, "string");
|
|
engine.addProperty("string.resource.loader.repository.static", "false");
|
|
engine.addProperty("string.resource.loader.class",
|
|
"org.apache.velocity.runtime.resource.loader.StringResourceLoader");
|
|
engine.addProperty("string.resource.loader.repository.class",
|
|
"org.apache.velocity.runtime.resource.util.StringResourceRepositoryImpl");
|
|
engine.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogChute");
|
|
}
|
|
|
|
public ResponseEntity<ErrorResponse> throwErrorExceptionWithMessage(HttpStatus httpStatus, String errorCode, String errorTitle, String errorMessage) {
|
|
|
|
ErrorResponse errorResponse = new ErrorResponse();
|
|
errorResponse.setCode(errorCode);
|
|
errorResponse.setTitle(errorTitle);
|
|
errorResponse.setMessage(errorMessage);
|
|
|
|
return new ResponseEntity<>(errorResponse, new HttpHeaders(), httpStatus);
|
|
}
|
|
} |