Error Handling
អានប្រមាណ 2 នាទី · ធ្វើបច្ចុប្បន្នភាពនៅ 31 តុលា 2026
ជំពូកនេះមិនទាន់ត្រូវបានបកប្រែជាភាសាខ្មែរនៅឡើយទេ — សូមអានជាភាសាអង់គ្លេសខាងក្រោម។
Table of Contents
- Why Not Just Return
null ResponseStatusException- Using It in
TaskService - Problem Details Responses
- Trying It
- Beginner Pitfalls
1. Why Not Just Return null
GET /tasks/999
If task 999 doesn't exist, silently returning null with a 200 OK forces every
caller to remember to check, and gives no hint why there's no data. (Right now
orElseThrow() with no message is even worse: it produces a 500, which says "the
server is broken" when the truth is "that id isn't there".) An HTTP API has a better
tool: status codes.
2. ResponseStatusException
Spring's built-in way to say "stop here and answer with this status" is to throw a
ResponseStatusException. Throw it from anywhere in a request — controller or service
— and Spring turns it into the matching HTTP response.
| Status | Constant | Use for |
|---|---|---|
| 400 | HttpStatus.BAD_REQUEST | Malformed input the caller sent |
| 404 | HttpStatus.NOT_FOUND | Nothing at that id |
| 409 | HttpStatus.CONFLICT | The request clashes with current state (a duplicate title) |
Note: bigger apps usually define their own exception classes and map them all in one
@RestControllerAdvice. For one small API,ResponseStatusExceptionis enough.
3. Using It in TaskService
Replace the bare orElseThrow() and add the duplicate check:
private Task getTask(Long id) {
return taskRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Task " + id + " not found"));
}
private void ensureTitleIsFree(String title) {
if (taskRepository.existsByTitle(title)) {
throw new ResponseStatusException(HttpStatus.CONFLICT, "A task titled '" + title + "' already exists");
}
}
Call ensureTitleIsFree(request.title()) at the top of create, and in update only
when the title is actually changing:
if (request.title() != null && !request.title().equals(task.getTitle())) {
ensureTitleIsFree(request.title());
task.setTitle(request.title());
}
The database's UNIQUE constraint on title is the real guarantee; this check just
turns the common case into a friendly 409 instead of a database error.
4. Problem Details Responses
By default Spring hides the exception message from error bodies. Turn on problem details — a standard JSON error format (RFC 9457) — so the message reaches the caller:
# src/main/resources/application.properties
spring.mvc.problemdetails.enabled=true
5. Trying It
curl -i http://localhost:8080/tasks/999
curl -i -X POST http://localhost:8080/tasks -H "Content-Type: application/json" \
-d '{"title":"Persisted task"}'
curl -i -X POST http://localhost:8080/tasks -H "Content-Type: application/json" \
-d '{"title":"Persisted task"}'
The first returns 404 with:
{
"detail": "Task 999 not found",
"instance": "/tasks/999",
"status": 404,
"title": "Not Found"
}
Posting the same title twice returns 201, then 409.
6. Beginner Pitfalls
- Returning
nullor an empty body for "not found". The caller gets200and has to guess. Throw, and let the status code do the talking. - Letting an unexpected exception escape as a
500.500should mean our bug. A missing record or a duplicate is the caller's situation — use404/409. - Using
400for everything. The status code is part of the API:404means "no such thing",409means "clashes with existing data",400means "your input is malformed".
Official docs:
តើជំពូកនេះមានប្រយោជន៍ទេ?
ទទួលមេរៀនថ្មីៗ
ចូលរួមឆានែល Telegram ដើម្បីដឹងនៅពេលមានជំពូក ឬស៊េរីថ្មី ឬតាមដានតាម RSS។
ទិញកាហ្វេជូនខ្ញុំ
ចូលចិត្តមេរៀននេះមែនទេ? សូមពិចារណាទិញកាហ្វេជូនខ្ញុំមួយកែវ ដើម្បីគាំទ្រការបង្កើតមាតិកាឥតគិតថ្លៃបន្ថែមទៀត!