Remote frontend nginx now forwards API requests to Django so login and registration work when the host proxy only routes to port 8080. Co-authored-by: Cursor <cursoragent@cursor.com>
46 lines
1.6 KiB
JavaScript
46 lines
1.6 KiB
JavaScript
export function formatApiErrorData(data) {
|
|
if (!data) return ''
|
|
if (typeof data === 'string' && data.trim()) {
|
|
const text = data.trim()
|
|
if (text.startsWith('<') && text.includes('</html>')) {
|
|
const title = text.match(/<title>([^<]+)<\/title>/i)?.[1]?.trim()
|
|
if (title) return title
|
|
return 'Сервер вернул HTML вместо JSON. Проверьте проксирование /api на backend.'
|
|
}
|
|
return text
|
|
}
|
|
if (typeof data?.detail === 'string' && data.detail.trim()) return data.detail.trim()
|
|
if (Array.isArray(data?.non_field_errors) && data.non_field_errors.length) {
|
|
return data.non_field_errors.join('; ')
|
|
}
|
|
if (typeof data === 'object') {
|
|
return Object.entries(data)
|
|
.map(([key, value]) => {
|
|
if (Array.isArray(value)) return `${key}: ${value.join(', ')}`
|
|
if (typeof value === 'string') return `${key}: ${value}`
|
|
return `${key}: ${JSON.stringify(value)}`
|
|
})
|
|
.join('; ')
|
|
}
|
|
return ''
|
|
}
|
|
|
|
export function normalizeApiError(error) {
|
|
const status = error?.response?.status ?? error?.status ?? null
|
|
const data = error?.response?.data ?? error?.data
|
|
const formatted = formatApiErrorData(data)
|
|
let message = formatted || 'Произошла ошибка запроса.'
|
|
|
|
if (!formatted && typeof data?.error === 'string' && data.error.trim()) {
|
|
message = data.error
|
|
} else if (!formatted && typeof error?.message === 'string' && error.message.trim()) {
|
|
message = error.message
|
|
}
|
|
|
|
const normalized = new Error(message)
|
|
normalized.status = status
|
|
normalized.data = data
|
|
normalized.original = error
|
|
return normalized
|
|
}
|