#Exception
1 messages · Page 1 of 1 (latest)
<@&1004656351647117403> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
In Spring Boot, you can throw a NoSuchElementException if there is nothing about the requested thing in your database. This exception is commonly used to indicate that an element does not exist in a collection or database.
You can simply throw this exception in your code like this:
throw new NoSuchElementException("Requested element not found in the database");
There is no need to create a custom exception for this specific case as the NoSuchElementException already serves this purpose.
You mean what to put inside the HTTP response on a spring application?
or you just want an exception to throw if the select doesn't return any result? in that case NoSuchElementException is a good choice
And which layer?
An absence does not always need to imply an exceptional circumstance
yep like this
this too, I mean i will have a function, you know findById from db?
and then .orElseThrow will throw an exception
or it's not a good idea
Like this:
private Category getByIdOrElseThrow(Long id) {
return categoryRepository.findById(id).orElseThrow(() ->
new NoSuchElementException("Category not found!"));
}
what do you mean?
In a Repository you should just return Optional<Category> and not throw an Exception on the absence for example
okay just a second
If it is a spring project or anything else that implements HTTP requests then the error should be added in the HTTP response:
ResponseEntity.status(HttpStatus.NOT_FOUND)
If you throw an exception when when you execute your query, then you will have to handle the exception when you call that method and return the thing i've wrote up here
public ResponseEntity<?> viewOne(Long id) {
Category category = getByIdOrElseThrow(id);
if (category == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
CategoryResDTO categoryResDTO = entityToResDTO(category);
return ResponseEntity.ok(categoryResDTO);
}
private Category getByIdOrElseThrow(Long id) {
Optional<Category> category = categoryRepository.findById(id);
return category.orElse(null);
}
what do you think about it?
or could i do it better?
what do you think bro
public ResponseEntity<?> viewOne(Long id) {
try {
Category category = getByIdOrElseThrow(id);
CategoryResDTO categoryResDTO = entityToResDTO(category);
return ResponseEntity.ok(categoryResDTO);
} catch (NoSuchElementException ex) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
private Category getByIdOrElseThrow(Long id) {
return categoryRepository.findById(id)
.orElseThrow(() -> new NoSuchElementException("Category not found with id: " + id));
}
or like this?
getByIdOrElseThrow is a bit odd given you don't throw
And I'd just have the service call throw some flavour of ResourceNotFoundException, and define an exception handler + enable problem details in spring boot.
you mean handling this exception in Exception Handler class?
Dude i'm really not understanding what scope is this project placed
based on what you want to achieve throwing an exception or building a response are both methods to handle things that returns something different from what they should, but they have different apply. Http response are used when you need to comunicate the client what is the result of his request, exception are used internally and you will have to handle them by yourself
you can just use @ExceptionHandler on a method in your controller. But you seem to be jumping layers in your question which is why I asked about it.
I have Controller interface and class that implements it then service interface with its class
and i wll call service in controller class
you mean this?
at times we're talking about controller, at times services, something repository. A bit what tvrsier also mentioned.
@dull palm
Your question has been closed due to inactivity.
If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.
Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.
When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.
Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.
With enough info, someone knows the answer for sure 👍
i tried this one
can u tell me if i did something wrong
private Category getByIdOrElseThrow(Long id) {
return categoryRepository.findById(id).orElseThrow(() ->
new NoSuchElementException("Category not found!"));
}
@org.springframework.web.bind.annotation.ExceptionHandler(NoSuchElementException.class)
public HttpEntity<?> handling(NoSuchElementException ex) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(ex.getMessage());
}
i got this response from postman:
{
"timestamp": "2024-02-25T09:34:17.069+00:00",
"status": 404,
"error": "Not Found",
"path": "/api/category/view/1"
}