Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.swiz.bcs.controller;

import com.swiz.bcs.exception.AuthorNotFoundException;
import com.swiz.bcs.exception.BookNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -13,5 +14,9 @@ public class GlobalControllerAdvice {
public ResponseEntity<String> handleBookNotFoundException(BookNotFoundException e){
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
}
@ExceptionHandler(AuthorNotFoundException.class)
public ResponseEntity<String> handleAuthorNotFoundException(AuthorNotFoundException e){
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.swiz.bcs.exception;

public class AuthorNotFoundException extends RuntimeException{
public AuthorNotFoundException(String message){
super(message);
}
}

4 changes: 3 additions & 1 deletion src/main/java/com/swiz/bcs/service/AuthorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import com.swiz.bcs.dto.AuthorDTO;
import com.swiz.bcs.entity.Author;
import com.swiz.bcs.exception.AuthorNotFoundException;
import com.swiz.bcs.repository.AuthorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -28,7 +29,8 @@ public List<Author> findAllAuthors() {
}

public Author findAuthorById(Long id) {
return authorRepository.findById(id).orElse(null);
return authorRepository.findById(id)
.orElseThrow(() -> new AuthorNotFoundException("Author not found with ID: "+id));
}

// Other author-related operations
Expand Down