من انتهاكات القيود إلى أخطاء API
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
return meaningful HTTP errors for violations.
الموضوعات المخطط لها
- A violation reaches Spring as
DataIntegrityViolationException(for exampleDuplicateKeyException). - Duplicates mean 409 Conflict; invalid data means 400 or 422.
- Catch it in one place: a
@RestControllerAdviceor the service where you can act. - Read the constraint name from the cause to choose the message (verify the exception path for your driver version).
- Never leak SQL text to the client; use the problem-details format from earlier modules.
تفاصيل المخطط المبدئي
عرض عملي
@ExceptionHandler(DataIntegrityViolationException.class)
ProblemDetail conflict(DataIntegrityViolationException e) {
return ProblemDetail.forStatusAndDetail(HttpStatus.CONFLICT, "SKU already exists");
}اختبر فهمك
Which HTTP status for a duplicate SKU?
أظهر الإجابة
409 Conflict.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.