From 146a0fb8b0e90f9196e569152f649baf60d6cc8f Mon Sep 17 00:00:00 2001
From: Joaquín Reñé <jrene@curisit.net>
Date: Tue, 07 Oct 2025 14:52:57 +0000
Subject: [PATCH] #4410 - Comments on classes

---
 securis/src/main/java/net/curisit/securis/DefaultExceptionHandler.java |   47 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 46 insertions(+), 1 deletions(-)

diff --git a/securis/src/main/java/net/curisit/securis/DefaultExceptionHandler.java b/securis/src/main/java/net/curisit/securis/DefaultExceptionHandler.java
index 7726c7d..6605752 100644
--- a/securis/src/main/java/net/curisit/securis/DefaultExceptionHandler.java
+++ b/securis/src/main/java/net/curisit/securis/DefaultExceptionHandler.java
@@ -1,3 +1,6 @@
+/*
+ * Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved.
+ */
 package net.curisit.securis;
 
 import jakarta.persistence.EntityManager;
@@ -17,18 +20,49 @@
 import net.curisit.securis.services.exception.SeCurisServiceException;
 import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
 
+/**
+* DefaultExceptionHandler
+* <p>
+* JAX-RS {@link ExceptionMapper} that normalizes error responses across the API.
+* It also makes a best-effort to rollback and close a request-scoped {@link EntityManager}
+* if still open.
+*
+* <p>Response strategy:
+* <ul>
+* <li>{@link ForbiddenException} → 401 UNAUTHORIZED with app-specific error headers.</li>
+* <li>{@link SeCurisServiceException} → 418 (custom) with app error headers.</li>
+* <li>Other exceptions → 500 with generic message and request context logging.</li>
+* </ul>
+*
+* Headers:
+* <ul>
+* <li>{@code X-SECURIS-ERROR-MSG}</li>
+* <li>{@code X-SECURIS-ERROR-CODE}</li>
+* </ul>
+*
+* @author JRA
+* Last reviewed by JRA on Oct 6, 2025.
+*/
 @Provider
 public class DefaultExceptionHandler implements ExceptionMapper<Exception> {
+	
 	private static final Logger LOG = LogManager.getLogger(DefaultExceptionHandler.class);
-
+	
+	/** Default status code used for application-defined errors. */
 	public static final int DEFAULT_APP_ERROR_STATUS_CODE = 418;
+	
+	/** Header name carrying a human-readable error message. */
 	public static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG";
+	
+	/** Header name carrying a symbolic application error code. */
 	public static final String ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE";
 
+	/** Default constructor (logs instantiation). */
 	public DefaultExceptionHandler() {
 		LOG.info("Creating DefaultExceptionHandler ");
 	}
 
+	// Context objects injected by the runtime
 	@Context
 	HttpServletRequest request;
 	@Context
@@ -36,6 +70,12 @@
 	@Context
 	EntityManager em;
 
+	/**
+	* toResponse
+	* <p>
+	* Map a thrown exception to an HTTP {@link Response}, releasing the {@link EntityManager}
+	* if present.
+	*/
 	@Override
 	public Response toResponse(Exception e) {
 		releaseEntityManager();
@@ -57,6 +97,11 @@
 		return Response.serverError().header(ERROR_MESSAGE_HEADER, "Unexpected error: " + e.toString()).type(MediaType.APPLICATION_JSON).build();
 	}
 
+	/**
+	* releaseEntityManager
+	* <p>
+	* Best-effort cleanup: rollback active transaction (if joined) and close the {@link EntityManager}.
+	*/
 	private void releaseEntityManager() {
 		try {
 			if (em != null && em.isOpen()) {

--
Gitblit v1.3.2