File tree Expand file tree Collapse file tree 5 files changed +81
-1
lines changed
src/main/java/com/kodluyoruz/subscription Expand file tree Collapse file tree 5 files changed +81
-1
lines changed Original file line number Diff line number Diff line change 11package com .kodluyoruz .subscription .controllers ;
22
33import com .kodluyoruz .subscription .contracts .requests .SubscriptionPaymentRequest ;
4+ import com .kodluyoruz .subscription .exceptions .InternalServerErrorException ;
45import org .springframework .beans .factory .annotation .Autowired ;
56import org .springframework .http .ResponseEntity ;
67import 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments