#Exception

1 messages · Page 1 of 1 (latest)

dull palm
#

What kind of exception can i throw if there is nothing about requested thing in my db?
Should i create a new one for myself or there is already such kind of exception to use in spring boot application?

raven jasperBOT
#

<@&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.

worldly island
#

or you just want an exception to throw if the select doesn't return any result? in that case NoSuchElementException is a good choice

gleaming cipher
#

And which layer?

#

An absence does not always need to imply an exceptional circumstance

dull palm
#

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!"));
}

dull palm
gleaming cipher
#

In a Repository you should just return Optional<Category> and not throw an Exception on the absence for example

dull palm
#

okay just a second

worldly island
#

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

dull palm
# worldly island If it is a spring project or anything else that implements HTTP requests then th...

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?

dull palm
#

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?

gleaming cipher
#

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.

dull palm
#

you mean handling this exception in Exception Handler class?

worldly island
#

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

gleaming cipher
dull palm
#

and i wll call service in controller class

gleaming cipher
#

at times we're talking about controller, at times services, something repository. A bit what tvrsier also mentioned.

raven jasperBOT
#

@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 👍

dull palm
#

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"
}