方法 2 - HackSoft 推荐的方式(续)
{
"message": "User is not eligible",
"code": "USER_NOT_ELIGIBLE",
"extra": {
"reason": "account_not_verified"
}
}
前端可以根据 code 进行不同的处理:
// 前端代码示例
if (error.code === "USER_NOT_ELIGIBLE") {
showVerificationPrompt();
} else if (error.code === "PAYMENT_FAILED") {
showPaymentRetryDialog();
}
4. 重新实现 DRF 的异常处理器
如果你需要更多控制,可以完全重新实现,而不是重用 DRF 的: