From e08dd8f53681575e64ba11a059a331619e4760f0 Mon Sep 17 00:00:00 2001 From: yoganandhan Date: Wed, 8 Oct 2025 16:57:52 +0530 Subject: [PATCH] changes in error handling from front end done --- .../gcc/crslogs/controller/ApiController.java | 51 +++- .../service/BirthAndDeathDetailsService.java | 1 - .../templates/modules/crslogspages/birth.html | 231 +---------------- .../templates/modules/crslogspages/death.html | 245 +----------------- 4 files changed, 51 insertions(+), 477 deletions(-) diff --git a/src/main/java/com/gcc/crslogs/controller/ApiController.java b/src/main/java/com/gcc/crslogs/controller/ApiController.java index ec6f6d7..36f6e97 100644 --- a/src/main/java/com/gcc/crslogs/controller/ApiController.java +++ b/src/main/java/com/gcc/crslogs/controller/ApiController.java @@ -7,11 +7,12 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.HashMap; - +import java.util.LinkedHashMap; import java.util.Map; import java.util.Random; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.jdbc.core.JdbcTemplate; @@ -21,6 +22,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; import com.fasterxml.jackson.databind.ObjectMapper; @@ -174,7 +176,11 @@ public class ApiController { // try { // Map result = birthAndDeathDetailsService.fetchBirthCertificate(dob, sex, regno, childname, fathername, mothername); // return ResponseEntity.ok(result); + + + // } catch (Exception e) { + // e.printStackTrace(); // Map error = new HashMap<>(); // error.put("status", false); @@ -212,14 +218,34 @@ public ResponseEntity fetchBirthCertificateSecurely(@RequestBody Map error = new HashMap<>(); + + String message = e.getMessage() != null ? e.getMessage().toLowerCase() : ""; + + if (message.contains("404")) { + error.put("status", false); + error.put("message", "No matching birth record found."); + return ResponseEntity.status(404).body(error); + } else if (message.contains("400")) { + error.put("status", false); + error.put("message", "Invalid request. Please check your input details."); + return ResponseEntity.status(400).body(error); + } else if (message.contains("socket") || message.contains("connection")) { error.put("status", false); - error.put("message", "Something went wrong: " + e.getMessage()); + error.put("message", "Connection issue. Please try again later."); + return ResponseEntity.status(503).body(error); + } else { + error.put("status", false); + error.put("message", "Something went wrong. Please try again later."); return ResponseEntity.status(500).body(error); } } +} + + -// from external api restapiservices + +// from external api restapiservices death certificate @PostMapping("/death-details") public ResponseEntity fetchDeathCertificateSecurely(@RequestBody Map params) { @@ -247,10 +273,27 @@ public ResponseEntity fetchDeathCertificateSecurely(@RequestBody Map error = new HashMap<>(); + +String message = e.getMessage() != null ? e.getMessage().toLowerCase() : ""; + + if (message.contains("404")) { + error.put("status", false); + error.put("message", "No matching death record found."); + return ResponseEntity.status(404).body(error); + } else if (message.contains("400")) { + error.put("status", false); + error.put("message", "Invalid request. Please check your input details."); + return ResponseEntity.status(400).body(error); + } else if (message.contains("socket") || message.contains("connection")) { error.put("status", false); - error.put("message", "Something went wrong: " + e.getMessage()); + error.put("message", "Connection issue. Please try again later."); + return ResponseEntity.status(503).body(error); + } else { + error.put("status", false); + error.put("message", "Something went wrong. Please try again later."); return ResponseEntity.status(500).body(error); } + } } diff --git a/src/main/java/com/gcc/crslogs/service/BirthAndDeathDetailsService.java b/src/main/java/com/gcc/crslogs/service/BirthAndDeathDetailsService.java index f5c8915..977e7ee 100644 --- a/src/main/java/com/gcc/crslogs/service/BirthAndDeathDetailsService.java +++ b/src/main/java/com/gcc/crslogs/service/BirthAndDeathDetailsService.java @@ -170,7 +170,6 @@ public class BirthAndDeathDetailsService { String rawJson = response.getBody(); ObjectMapper mapper = new ObjectMapper(); Map result = mapper.readValue(rawJson, new TypeReference>() {}); - // ✅ Only keep records that actually have a PDF link // if (result.containsKey("certificate_details")) { // List> details = (List>) result.get("certificate_details"); diff --git a/src/main/resources/templates/modules/crslogspages/birth.html b/src/main/resources/templates/modules/crslogspages/birth.html index ffeac5f..6668054 100644 --- a/src/main/resources/templates/modules/crslogspages/birth.html +++ b/src/main/resources/templates/modules/crslogspages/birth.html @@ -591,98 +591,11 @@ body { - - - +
@@ -703,9 +616,6 @@ body { - - - @@ -725,28 +635,7 @@ body { - - - - - - - @@ -1204,128 +1093,12 @@ function fetchBirthData() { if (xhr.responseJSON && xhr.responseJSON.message) { message = xhr.responseJSON.message; } - alert("Error: " + message); + alert(message); } }); } - - - -// this function only using in live - -function fetchDeathData() { -const dod = document.getElementById('dateofdeath').value; -const gender = document.querySelector('input[name="deathgender"]:checked')?.value; -const captchaInput = document.getElementById('death-captcha-input'); - -// console.log("gender:", gender); - -// Validate Date of Birth -if (!dod) { - Swal.fire({ - icon: 'warning', - title: 'Missing Date of Birth', - text: 'Please enter your Date of Birth.' - }); - return; -} - -// Validate Gender -if (!gender) { - Swal.fire({ - icon: 'warning', - title: 'Missing Gender Selection', - text: 'Please select a gender.' - }); - return; -} - - -if (!validateCaptcha('death')) { - //captchaInput.classList.add('is-invalid'); - //captchaInput.focus(); - return; -} else { - captchaInput.classList.remove('is-invalid'); -} - -const formattedDod = dod.split("-").reverse().join("/"); // Convert yyyy-MM-dd to dd/MM/yyyy - -// console.log("dod:", formattedDod); -$('#pageLoader').show(); - -$.ajax({ - url: '/crs/api/death-details', - method: 'GET', - data: { - dod: formattedDod, - sex: gender.charAt(0).toUpperCase() - }, - success: function (response) { - $('#pageLoader').hide(); - - if (response && response.DEATH && response.DEATH.length > 0) { - let html = '' + - '' + - ''; - - response.DEATH.forEach((row ,index) => { - html += ` - - - - - - - `; - }); - - html += '
S.NoReg NoPerson NameFather NameMother NameDODCertificate Link
${index + 1}${row.REGNO}${row.DEATHPERSONNAME}${row.FATHERNAME}${row.MOTHERNAME}${row.DOD}${row.ENGLISHURL ? `View` : 'N/A'}
'; - $('#deathModalBody').html(html); - - // Delay initialization to ensure DOM is ready - setTimeout(() => { - $('#deathDataTable').DataTable({ - responsive: true, - pagingType: "full_numbers", // Buttons for pagination - language: { - search: "🔍 Search: ", - emptyTable: "No records found", - paginate: { - previous: "", - next: "" - } - }, - columnDefs: [ - { orderable: false, targets: 0 } // Disable sorting on S.No - ] - }); - }, 100); - } else { - $('#deathModalBody').html('

No deathrecords found.

'); - } - - const modal = new bootstrap.Modal(document.getElementById('deathModal')); - modal.show(); - }, - error: function (xhr) { - $('#pageLoader').hide(); - let message = "Something went wrong."; - if (xhr.responseJSON && xhr.responseJSON.message) { - message = xhr.responseJSON.message; - } - alert("Error: " + message); - } -}); - -} - - - - function saveCertificateLog(regNo, eventDate, url, service){ $.ajax({ url: '/crs/api/saveCertificateLogs', diff --git a/src/main/resources/templates/modules/crslogspages/death.html b/src/main/resources/templates/modules/crslogspages/death.html index 0c78999..b9f5d1d 100644 --- a/src/main/resources/templates/modules/crslogspages/death.html +++ b/src/main/resources/templates/modules/crslogspages/death.html @@ -500,98 +500,7 @@ body {
-
- - -
- - - - +
@@ -702,30 +611,6 @@ body { wait...

- - - - - - - - - - @@ -969,132 +854,6 @@ document.addEventListener("DOMContentLoaded", function () { - -function fetchBirthData() { - - -const dob = document.getElementById('dateofbirth').value; -const gender = document.querySelector('input[name="gender"]:checked')?.value; -const captchaInput = document.getElementById('birth-captcha-input'); - -//console.log("gender:", gender); - -// Validate Date of Birth - if (!dob) { - Swal.fire({ - icon: 'warning', - title: 'Missing Date of Birth', - text: 'Please enter your Date of Birth.' - }); - return; - } - - // Validate Gender - if (!gender) { - Swal.fire({ - icon: 'warning', - title: 'Missing Gender Selection', - text: 'Please select a gender.' - }); - return; - } - -// CAPTCHA validation first -// Validate CAPTCHA -if (!validateCaptcha('birth')) { - //captchaInput.classList.add('is-invalid'); - //captchaInput.focus(); - return; -} else { - captchaInput.classList.remove('is-invalid'); -} - -const formattedDob = dob.split("-").reverse().join("/"); // Convert yyyy-MM-dd to dd/MM/yyyy -//console.log("dob:", formattedDob); -$('#pageLoader').show(); - -$.ajax({ - url: '/crs/api/birth-details', - method: 'GET', - data: { - dob: formattedDob, - sex: gender.charAt(0).toUpperCase() - }, - success: function (response) { - $('#pageLoader').hide(); - - - if (response && response.BIRTH && response.BIRTH.length > 0) { - - console.log("birthdetails:",response); - let html = ` - - - - - - - - - - - - - `; - - response.BIRTH.forEach((row, index) => { - html += ` - - - - - - - - `; - }); - - html += `
S.NoReg NoChild NameFatherMotherDOBCertificate Link
${index + 1}${row.REGNO}${row.CHILDNAME}${row.FATHERNAME}${row.MOTHERNAME}${row.DOB}${row.ENGLISHURL ? `View` : 'N/A'}
`; - $('#birthModalBody').html(html); - - setTimeout(() => { - $('#birthDataTable').DataTable({ - responsive: true, - pagingType: "full_numbers", // Buttons for pagination - language: { - search: "🔍 Search: ", - emptyTable: "No records found", - paginate: { - previous: "", - next: "" - } - }, - columnDefs: [ - { orderable: false, targets: 0 } // Disable sorting on S.No - ] - }); - }, 100); - } - else { - $('#birthModalBody').html('

No birth records found.

'); - } - - const modal = new bootstrap.Modal(document.getElementById('birthModal')); - modal.show(); - }, - error: function (xhr) { - $('#pageLoader').hide(); - let message = "Something went wrong."; - if (xhr.responseJSON && xhr.responseJSON.message) { - message = xhr.responseJSON.message; - } - alert("Error: " + message); - } -}); - -} - // using fetchdeathdata function in live // function fetchDeathData() { @@ -1324,7 +1083,7 @@ $.ajax({ if (xhr.responseJSON && xhr.responseJSON.message) { message = xhr.responseJSON.message; } - alert("Error: " + message); + alert(message); } });