Below is the service class and controller class for the spring boot application
@Autowired
private RecordRepository recordRepository;
public void insertRecord(String dest, Integer num) {
try {
List<Record> b = recordRepository.findByDest(dest);
ArrayList<Integer> numList = new ArrayList<>();
Record record = new Record();
if (!b.isEmpty()) {
for (Record r : b) {
numList.add(r.num());
}
if (!numList.contains(num)) {
List<Record> counts = recordRepository.findAll();
ArrayList<Integer> countList = new ArrayList<>();
for (Record r1 : counts) {
countList.add(r1.getId());
}
Collections.sort(countList);
int countInsert = countList.get(countList.size() - 1);
record.setId(countInsert + 1);
record.setDest(dest);
record.setNum(num);
recordRepository.save(record);
} else {
throw new DuplicateNumberException("Number already exists.");
}
} else {
throw new NotFoundException("No records found for the given String.");
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("An unexpected error occurred while processing the request.", e);
}
}
@Autowired private RecordService service;
@PostMapping("/add")
public ResponseEntity<String> insertRecord(@RequestBody Map<String, String> payload) {
try {
String dest = payload.get("xyz");
Integer num = Integer.parseInt(payload.get("num"));
service.insertRecord(dest, num);
return ResponseEntity.ok("Record inserted successfully.");
}catch (DuplicateNumberException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Duplicate number. Record not inserted.");
} catch (NotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No records found for the given String.");
} catch (NumberFormatException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid number format.");
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("An unexpected error occurred while processing the request.");
}
}
Whenever a call is made and the response should be either DuplicateNumberException or NotFoundException, I am getting reponse code 500 Internal server error.
I have tried including the if conditions in try block and catch the exceptions but it is breaking the flow of the app.
Take a close look at your code. You have a nested try/catch
. In your outer try/catch,
you're catching Exception
, which is a superclass of every exception. Therefore, any exception thrown by the inner catch will be caught by the outer, causing your method to throw a RuntimeException
.
You need to remove the outer try/catch
.
public void insertRecord(String dest, Integer num) {
List<Record> b = recordRepository.findByDest(dest);
ArrayList<Integer> numList = new ArrayList<>();
Record record = new Record();
if (!b.isEmpty()) {
for (Record r : b) {
numList.add(r.num());
}
if (!numList.contains(num)) {
List<Record> counts = recordRepository.findAll();
ArrayList<Integer> countList = new ArrayList<>();
for (Record r1 : counts) {
countList.add(r1.getId());
}
Collections.sort(countList);
int countInsert = countList.get(countList.size() - 1);
record.setId(countInsert + 1);
record.setDest(dest);
record.setNum(num);
recordRepository.save(record);
} else {
throw new DuplicateNumberException("Number already exists.");
}
} else {
throw new NotFoundException("No records found for the given String.");
}
}