fix: [hr] 사원 등록/수정 에러 메시지 화면 표시 개선
- API 컨트롤러 store/update/destroy에 try-catch 추가 - debug 모드에서 상세 에러 메시지 포함 응답 - create/edit 뷰에 showToast 기반 에러 표시 추가 - 422 validation 에러 필드별 메시지 표시 - 500 서버 에러 시 사용자 친화적 메시지 표시
This commit is contained in:
@@ -196,13 +196,48 @@ class="px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg transition-
|
||||
@push('scripts')
|
||||
<script>
|
||||
document.body.addEventListener('htmx:afterRequest', function(event) {
|
||||
if (event.detail.successful) {
|
||||
try {
|
||||
const response = JSON.parse(event.detail.xhr.responseText);
|
||||
if (response.success) {
|
||||
window.location.href = '{{ route('hr.employees.show', $employee->id) }}';
|
||||
if (event.detail.elt.id !== 'employeeForm') return;
|
||||
|
||||
const xhr = event.detail.xhr;
|
||||
try {
|
||||
const response = JSON.parse(xhr.responseText);
|
||||
|
||||
if (response.success) {
|
||||
showToast(response.message || '사원 정보가 수정되었습니다.', 'success');
|
||||
window.location.href = '{{ route('hr.employees.show', $employee->id) }}';
|
||||
return;
|
||||
}
|
||||
|
||||
let msg = response.message || '저장에 실패했습니다.';
|
||||
if (response.error) msg += '\n' + response.error;
|
||||
showToast(msg, 'error', 5000);
|
||||
} catch (e) {
|
||||
if (!event.detail.successful) {
|
||||
showToast('서버 오류가 발생했습니다. (HTTP ' + xhr.status + ')', 'error', 5000);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
document.body.addEventListener('htmx:responseError', function(event) {
|
||||
if (event.detail.elt.id !== 'employeeForm') return;
|
||||
|
||||
const xhr = event.detail.xhr;
|
||||
try {
|
||||
const response = JSON.parse(xhr.responseText);
|
||||
|
||||
if (xhr.status === 422 && response.errors) {
|
||||
const messages = [];
|
||||
for (const field in response.errors) {
|
||||
messages.push(response.errors[field].join(', '));
|
||||
}
|
||||
} catch (e) {}
|
||||
showToast(messages.join('\n'), 'error', 6000);
|
||||
} else {
|
||||
let msg = response.message || '오류가 발생했습니다.';
|
||||
if (response.error) msg += '\n' + response.error;
|
||||
showToast(msg, 'error', 5000);
|
||||
}
|
||||
} catch (e) {
|
||||
showToast('서버 오류가 발생했습니다. (HTTP ' + xhr.status + ')', 'error', 5000);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user