Skip to content

Commit

Permalink
added Custom Exception and some minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sayeedajmal committed Apr 26, 2024
1 parent 55b4887 commit a39f1bc
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.web.bind.annotation.RestController;

import com.strong.speedsyncrestful.Entity.Vehicle;
import com.strong.speedsyncrestful.Exception.SpeedSyncException;
import com.strong.speedsyncrestful.Service.VehicleService;

@RestController
Expand All @@ -19,8 +20,8 @@ public class VehicleController {
private VehicleService vehicleService;

@PostMapping("challan")
public ResponseEntity<Void> regVehicles(@RequestBody Vehicle vehicle) {
vehicleService.regVehicle(vehicle);
public ResponseEntity<Void> generateChallan(@RequestBody Vehicle vehicle) throws SpeedSyncException {
vehicleService.challanSave(vehicle);
return new ResponseEntity<>(HttpStatus.CREATED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.strong.speedsyncrestful.Exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(SpeedSyncException.class)

public ResponseEntity<?> handleSpeedException(SpeedSyncException exception) {
SpeedRes response = new SpeedRes();
response.setMessage(exception.getLocalizedMessage());
response.setStatus(HttpStatus.CONFLICT.value());
response.setTimeStamp(System.currentTimeMillis());
return new ResponseEntity<>(response, HttpStatus.CONFLICT);
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/strong/speedsyncrestful/Exception/SpeedRes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.strong.speedsyncrestful.Exception;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@Getter
@Setter
public class SpeedRes {
private String Message;
private Integer Status;
private long TimeStamp;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.strong.speedsyncrestful.Exception;


public class SpeedSyncException extends Exception {

public SpeedSyncException() {
super();
}

public SpeedSyncException(String Message) {
super(Message);
}

public SpeedSyncException(String Message, Throwable throwable) {
super(Message, throwable);
}

public SpeedSyncException(Throwable throwable) {
super(throwable);
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.strong.speedsyncrestful.Service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.strong.speedsyncrestful.Entity.Vehicle;
import com.strong.speedsyncrestful.Exception.SpeedSyncException;
import com.strong.speedsyncrestful.Repository.VehicleRepo;

import lombok.NonNull;
Expand All @@ -16,12 +15,13 @@ public class VehicleService {
@Autowired
private VehicleRepo vehicleRepo;

public void regVehicle(@NonNull Vehicle vehicle) {
public void challanSave(@NonNull Vehicle vehicle) throws SpeedSyncException {
vehicle.setTimestamp(System.currentTimeMillis());
vehicleRepo.save(vehicle);
try {
vehicleRepo.save(vehicle);
} catch (Exception ex) {
throw new SpeedSyncException("Failed to Generate Challan: " + ex.getMessage());
}
}

public List<Vehicle> getVehicles() {
return vehicleRepo.findAll();
}
}

0 comments on commit a39f1bc

Please sign in to comment.