Skip to content

Commit

Permalink
👌 chore: handle global exception #10
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Sep 17, 2022
1 parent f3654a3 commit acc8a62
Showing 1 changed file with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.phuocnguyen.app.ngxblobso2jwt.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sivaos.Configurer.CustomFilterRequest.TypeSafeRequest;
import com.sivaos.Model.ObjectEnumeration.Original.ComparatorType;
import com.sivaos.Model.Response.Extend.HttpStatusCodesResponseDTO;
import com.sivaos.Model.Response.Extend.StatusCodeResponseDTO;
import com.sivaos.Utility.CollectionsUtility;
import com.sivaos.Utils.DateUtils;
import com.sivaos.Utils.ObjectUtils;
import com.sivaos.Utils.UrlQueryUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -37,15 +42,24 @@ public static void onHandlerErrors(HttpServletRequest request, HttpServletRespon

Map<String, Object> map = new HashMap<>();

map.put("code", statuses.getCode());
map.put("header", statuses);
map.put("message", exception.getMessage());
map.put("path", request.getServletPath());
map.put("timestamp", new Date().getTime());
map.put("publish", DateUtils.feedStageAsString(new Date()));
map.put("class", clazz);
map.put("method", request.getMethod());
map.put("sessionId", request.getSession().getId());
Map<String, String> queries = TypeSafeRequest.snapTypeSafeRequest(request);
String urlShortCompleted = UrlQueryUtils.snapQuery(request.getServletPath(), CollectionsUtility.sortByKey(queries, ComparatorType.ASCENDING));

map.put(HandlerAttributes.CODE_ATTRIBUTE, statuses.getCode());
map.put(HandlerAttributes.HEADER_ATTRIBUTE, statuses);
map.put(HandlerAttributes.MESSAGE_ATTRIBUTE, exception.getMessage());
map.put(HandlerAttributes.PATH_ATTRIBUTE, urlShortCompleted);
map.put(HandlerAttributes.TIMESTAMP_ATTRIBUTE, new Date().getTime() / 1000);
map.put(HandlerAttributes.PUBLISH_ATTRIBUTE, DateUtils.feedStageAsString(new Date()));
map.put(HandlerAttributes.CLASS_ATTRIBUTE, clazz);
map.put(HandlerAttributes.METHOD_ATTRIBUTE, request.getMethod());
map.put(HandlerAttributes.DEBUG_MESSAGE_ATTRIBUTE, exception.getLocalizedMessage());

if (ObjectUtils.allNotNull(request.getSession())) {
map.put(HandlerAttributes.SESSION_ID_ATTRIBUTE, request.getSession().getId());
} else {
map.put(HandlerAttributes.SESSION_ID_ATTRIBUTE, request.getRequestedSessionId());
}

try {
ObjectMapper mapper = new ObjectMapper();
Expand All @@ -55,4 +69,17 @@ public static void onHandlerErrors(HttpServletRequest request, HttpServletRespon
throw new ServletException(e.getMessage(), e);
}
}

private static class HandlerAttributes {
public static final String CODE_ATTRIBUTE = "code";
public static final String HEADER_ATTRIBUTE = "header";
public static final String MESSAGE_ATTRIBUTE = "message";
public static final String PATH_ATTRIBUTE = "path";
public static final String TIMESTAMP_ATTRIBUTE = "timestamp";
public static final String PUBLISH_ATTRIBUTE = "publish";
public static final String CLASS_ATTRIBUTE = "class";
public static final String METHOD_ATTRIBUTE = "method";
public static final String DEBUG_MESSAGE_ATTRIBUTE = "debugMsg";
public static final String SESSION_ID_ATTRIBUTE = "sessionId";
}
}

0 comments on commit acc8a62

Please sign in to comment.