Skip to content

Commit 54ec6fb

Browse files
committed
Added error handlings
1 parent 6d50647 commit 54ec6fb

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

src/main/java/com/kodluyoruz/subscription/controllers/SubscriptionPaymentController.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.kodluyoruz.subscription.controllers;
22

33
import com.kodluyoruz.subscription.contracts.requests.SubscriptionPaymentRequest;
4+
import com.kodluyoruz.subscription.exceptions.InternalServerErrorException;
45
import org.springframework.beans.factory.annotation.Autowired;
56
import org.springframework.http.ResponseEntity;
67
import org.springframework.web.bind.annotation.*;
@@ -13,6 +14,11 @@ public class SubscriptionPaymentController {
1314
SubscriptionPaymentController subscriptionPaymentController;
1415
@PostMapping
1516
public ResponseEntity paySubscription(@RequestBody SubscriptionPaymentRequest subscriptionPaymentRequest, @PathVariable String id) {
16-
return ResponseEntity.noContent().build();
17+
try{
18+
return ResponseEntity.noContent().build();
19+
}catch (Exception e)
20+
{
21+
throw new InternalServerErrorException("Internal Server Error!");
22+
}
1723
}
1824
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.kodluyoruz.subscription.exceptions;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
import java.util.Date;
8+
9+
@Getter
10+
@Setter
11+
@Builder
12+
public class ExceptionDetails {
13+
14+
private String message;
15+
private Date timeStamp;
16+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.kodluyoruz.subscription.exceptions;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.http.converter.HttpMessageNotReadableException;
6+
import org.springframework.web.bind.annotation.ControllerAdvice;
7+
import org.springframework.web.bind.annotation.ExceptionHandler;
8+
9+
import java.util.Date;
10+
11+
@ControllerAdvice
12+
public class ExceptionsHandler {
13+
14+
@ExceptionHandler(ResourceNotFoundException.class)
15+
public ResponseEntity<Object> resourceNotFoundException(ResourceNotFoundException exception){
16+
ExceptionDetails details = new ExceptionDetails(exception.getMessage(), new Date());
17+
18+
return new ResponseEntity<>(details, HttpStatus.NOT_FOUND);
19+
}
20+
21+
@ExceptionHandler(InternalServerErrorException.class)
22+
public ResponseEntity<Object> internalServerErrorException(InternalServerErrorException exception){
23+
ExceptionDetails details = new ExceptionDetails(exception.getMessage(), new Date());
24+
25+
return new ResponseEntity<>(details, HttpStatus.INTERNAL_SERVER_ERROR);
26+
}
27+
28+
@ExceptionHandler(HttpMessageNotReadableException.class)
29+
public ResponseEntity<Object> httpMessageNotReadableException(HttpMessageNotReadableException exception){
30+
ExceptionDetails details = new ExceptionDetails("One of the required parameter is null!", new Date());
31+
32+
return new ResponseEntity<>(details, HttpStatus.BAD_REQUEST);
33+
}
34+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.kodluyoruz.subscription.exceptions;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ResponseStatus;
5+
6+
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
7+
public class InternalServerErrorException extends RuntimeException{
8+
9+
public InternalServerErrorException(String message){
10+
super(message);
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.kodluyoruz.subscription.exceptions;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ResponseStatus;
5+
6+
@ResponseStatus(value = HttpStatus.NOT_FOUND)
7+
public class ResourceNotFoundException extends RuntimeException{
8+
9+
public ResourceNotFoundException(String message){
10+
super(message);
11+
}
12+
}

0 commit comments

Comments
 (0)