μλ²μμ μ ν¨μ± κ²μ¬ μ€λ₯λ₯Ό μ‘μΌλ €κ³ ν©λλ€.
μνΈ:
axios.post('/formulas/create', {
name: "",
parts: ""
})
.then(
(response) => { console.log(response) },
(error) => { console.log(error) }
);
μ½μ λ‘κ·Έ μΆλ ₯
Error: Request failed with status code 422
at createError (app.js:6765)
at settle (app.js:29489)
at XMLHttpRequest.handleLoad (app.js:6600)
λ€νΈμν¬ ν μΆλ ₯
{"name":["μ΄λ¦ νλλ νμμ
λλ€."],"parts":["λΆν νλλ νμμ
λλ€."]}
λ΄ λ€νΈμν¬ νμ μλ΅μΈ JSON μμ μ ν¨μ± κ²μ¬κ° ν¬ν¨λ κ°μ²΄κ° νμλμ΄μΌ ν©λλ€. JS catch μΆλ ₯μ μ»λ κ² κ°μ΅λκΉ?
λν λ€μμ μλνμ΅λλ€.
axios.post('/formulas/create', {
name: "",
parts: ""
})
.then(response => {
console.log(response)
})
.catch(error => {
console.log(error)
});
κ°μ κ²°κ³Ό
μ¬κΈ° λμ κ°μ νκ²½μ μ¬μ©νλ©΄μ λ λ§μ μ¬λλ€μ΄ κ°μ λ¬Έμ λ₯Ό κ²ͺκ³ μλ κ² κ°λ€.
https://laracasts.com/discuss/channels/vue/issues-with-axios-catch-method
λλ μ νν κ°μ νκ²½μ κ°μ§κ³ μλ€. μ΄ μλ:
axios.post('/formulas/create', {
name: "",
parts: ""
})
.then(response => {
console.log(response)
})
.catch(error => {
console.log(error.response)
});
μμ μμ console.log(error)
μ console.log(error.response)
μμ catch
.
μ μ μΈν°μ
ν°λ₯Ό μ¬μ©νκ³ error.response
λ§ κ±°λΆν μλ μμ΅λλ€. λ¬Έμ λ console.log
κ° μ€λ₯λ₯Ό μΆλ ₯νλ €κ³ ν λ κ°μ²΄ κ΅¬μ‘°κ° μλ λ¬Έμμ΄ ννμ΄ μΈμλμ΄ .response
μμ±μ΄ νμλμ§ μλλ€λ κ²μ
λλ€.
response
μμ±λ§ κ±°λΆνλ λ°©λ²μ΄ κΆκΈνμ λΆλ€μ μν΄ λ€μκ³Ό κ°μ΄ ν©λλ€.
axios.interceptors.response.use((response) => {
return response;
}, function (error) {
// Do something with response error
if (error.response.status === 401) {
console.log('unauthorized, logging out ...');
auth.logout();
router.replace('/auth/login');
}
return Promise.reject(error.response);
});
μ€μν λΆλΆμ return Promise.reject(error.response);
λ€μ¬μ°κΈ° λλ μΆμνλ λμ μμ μ루μ μ΄ JS νμΌ κ΅¬λ¬Έμ μλ§μΌλ‘ λ§λλ κ²½μ° λμΌν κ²°κ³Όλ₯Ό μ°μΆν©λλ€. :)
.post('ajax/register/otp', this.registerData)
.then(function (response) {
return otpSent(response)
})
.catch(function (error) {
console.log(error.response);
});
@gopal-g μ μκ² μ μλμ§ μμμ΅λλ€.
@pedro-mass μ΄ κ²½μ° μλ² μΈ‘μμ μλ΅μ΄ μμ μ μμ΅λλ€. λ΄ μλ리μ€μμ μ€λ₯κ° λ°μνμ λ λ΄ μλ΅μ JSONμ΄μμ΅λλ€. error.response
μ¬μ©νμ¬ μλ΅μ μ»μ μ μμμ΅λλ€.
@gopal-g κ°λ° λꡬμμ λ€νΈμν¬ νμ λ³΄κ³ μμΌλ©΄ μλ΅μ λ³Ό μ μμ΅λλ€. μ°¨μ΄κ° μλ€λ©΄ 401μ λλ€.
@pedro-massμ λμΌν μ€λ₯κ° μμ΅λλ€.
μ¬κΈ° λ΄ μ½λκ° μμ΅λλ€
async function test () {
try {
let res = await axios.get('/xxxxx/xxxx')
} catch (e) {
console.log(e.response) // undefined
}
}
"error.response"λ₯Ό μ¬μ©νλ©΄ μλνμ§ μμ΅λλ€. λ¬Έμ λ λ°μ΄ν°λ² μ΄μ€ μλ²μ μ°κ²°μ λκ³ μΉ μλ²μμ μ€λ₯ 500μ λ°ννμ λ λ°μν©λλ€.
μλ΅ μ½λ: 500
μλ΅ λ³Έλ¬Έ:
{
"error": {
"statusCode": 500,
"name": "Error",
"message": "read ETIMEDOUT",
"code": "ETIMEDOUT",
"errno": "ETIMEDOUT",
"syscall": "read",
"stack": "Error: read ETIMEDOUT\n at exports._errnoException (util.js:1050:11)\n at TCP.onread (net.js:582:26)"
}
}
ν¬λ‘¬ μ½μ μ€λ₯:
Uncaught (in promise) Error: Request failed with status code 500
at createError (createError.js:15)
at settle (settle.js:18)
at XMLHttpRequest.handleLoad (xhr.js:77)
"axios": "^0.15.3"
catch
μΊμΉκ° μμ΅λλ€. μμ²μ΄ μ΄λ£¨μ΄ μ§κΈ° μ μ μ€λ₯κ° λ°μνλ©΄ catch
μλ err.response
μμ±μ΄ μμ΅λλ€. μλνλ©΄... μ.. . μλ΅μ΄ μμ΅λλ€. λ°λΌμ catch
λ λͺ¨λ μ€λ₯λ₯Ό ν¬μ°© @fabiorecife μ κ²½μ°μ κ°μ΄ λ€νΈμν¬ μ₯μ λ‘ μΈν΄ μλ΅μ΄ μλ κ²½μ° λ§μ°¬κ°μ§ μ
λλ€. νμ κ·Έλ° κ²μ μλλ―λ‘ err
λ₯Ό HTTP μ€λ₯λ‘ μ·¨κΈνμ§ λ§μμμ€.
@ alsofronie κ·Έλμ μλ₯Ό λ€μ΄ 413κ³Ό κ°μ λΉν μ μ€ν¨μ κ°μ΄ μ νΈνλ μ²λ¦¬ λ°©λ²(λ€λ₯Έ κ²κ³Ό ꡬλ³)μ 무μμ λκΉ?
λΉμ·ν λ¬Έμ κ° λ°μν©λλ€. Chrome devtoolsμ 401
κ° νμλκ³ μλ΅μ {"message":"Identity token has expired"}
μ΄κ³ μ λ catch( (error) => {...})
μ΄μ§λ§ error.responseλ λΉμ΄ μμ΅λλ€. GET
μ μ°κ²°λ μ ν OPTIONS
νΈμΆμ μνκ° 200
μ΄κΈ° λλ¬ΈμΈμ§ κΆκΈν©λλ€. μ΄ κ²½μ° μ΄λ»κ² ν΄μΌ νλ©° 401
μν μ½λλ₯Ό κ²μν μ μλ μ¬λ°λ₯Έ μμΉλ μ΄λμ
λκΉ?
@robbiemu μ μκ°μλ μ΄λ―Έ λ¬Έμνλμ΄ μλ€κ³ μκ°ν©λλ€: https://github.com/axios/axios#handling -errors
@mrchief μ΄κ² μ λν΄ κ°μ¬λ립λλ€. λ€μ ν λ² error.response
κ°μ²΄κ° μμ§λ§ νλλ undefined
μ΄κ³ response
λ΄λΆμ κ°μ λν λͺ¨λ νΈμΆμ
λλ€.
@paolavness μ¬κΈ°μ μΈκΈνλ λμ μ λ¬Έμ λ₯Ό μ¬λ κ²μ΄ μ’μ΅λλ€. λ§μ μκ³ νμ λ¬Έμ κ° μμ΅λλ€. μλλ©΄ StackOverflowμ λ¬Όμ΄λ³Ό μλ μμ΅λλ€.
@mrchief μ μ΄κ² λ«ν
λλ κ°μ λ¬Έμ κ° μμλ€. κΆκ·Ήμ μΌλ‘ μ€λ₯ μ½λλ₯Ό 401μμ 403μΌλ‘ λ³κ²½νκ³ λͺ¨λ κ²μ΄ μμλλ‘ μλνμ΅λλ€. λΉ μ€λ₯ κ°μ²΄μ λν μ΄μ λ₯Ό μΆμΈ‘νκ³ μμΌλ©° κ²°κ³Ό μλ°μ€ν¬λ¦½νΈ μ€λ₯λ 401 μ€λ₯μ λν μ€λͺ μμμ μ°Ύμ μ΄ λͺ λ Ήλ¬Έκ³Ό κ΄λ ¨μ΄ μμ΅λλ€.
"[A 401 μ€λ₯] μλ΅μλ μμ²λ 리μμ€μ μ μ©ν μ μλ μ±λ¦°μ§κ° ν¬ν¨λ WWW-Authenticate ν€λ νλκ° ν¬ν¨λμ΄μΌ ν©λλ€."
λ°λΌμ 401 μ€λ₯λ₯Ό νμ©νλ €λ©΄ www-authenticate ν€λ νλλ₯Ό ν¬ν¨ν΄μΌ ν©λλ€. μ΄ μμ μ μννμ§ μμΌλ €λ©΄ 403 μ€λ₯λ₯Ό μ¬μ©νμμμ€.
μ’μ μμΉ¨μ λλ€. μ΄ λ¬Έμ κ° μμμ§λ§ μλ²μμ corsλ₯Ό νμ±ννλλ‘ μνμΉ κ΅¬μ±μ λ³κ²½νμ¬ μμ νμ΅λλ€. μλ λ§ν¬λ₯Ό μ΄ν΄λ³΄μΈμ
console.log(JSON.stringify(error))
λ‘λ§ λͺ¨λ μλ΅μ λλ²κ·Έν μ μμ΅λλ€.
axios.post(URL)
.then((μλ΅) => {
// 무μ
})
.catch((μλ΅) => {
if (response.status == μ μλμ§ μμ) {
κ²½κ³ ('무λ¨')
}
})
@fabiorecife μμ μ€λͺ ν κ²κ³Ό λκ°μ λ¬Έμ κ°
.catch(error=>{
let errorObject=JSON.parse(JSON.stringify(error));
console.log(errorObject);
dispatch(authError(errorObject.response.data.error));
})
κ·Έλ¬λ©΄ errorObject
λ³μμ μμλ κ²°κ³Όκ° μμ±λ©λλ€.
@petruscurtu λλ κ°μ λ¬Έμ κ° μμ΅λλ€! κ·Έκ²μ λλ₯Ό μν΄ μλνμ§ μμ΅λλ€!
λλ κ·Έκ²μ΄ 'no Access-control-allow-origin'μ λμ§λ μ΄μ λ₯Ό λͺ¨λ₯΄μ§λ§ μλ²κ° κ·Έκ²μ μ€μ νμ΅λλ€! μλ΅ ν€λλ μ μμ
λλ€
ν΄κ²°λμμ΅λλ€. μΈμ¦ APIκ° Access-control-allow-originμ΄ μμ΄μΌ νλ μ¬λ°λ₯Έ ν€λμ μλ΅νμ§ μμμ΅λλ€.
@luxueyan μλ²μμ CORSλ₯Ό μ²λ¦¬νλ λ°©λ²μ λͺ¨λ₯΄κΈ° λλ¬Έμ λ¬Έμ κ° λ¬΄μμΈμ§ λͺ¨λ₯΄κ² μ΅λλ€. axiosμ κ΄ν ν μ½μ μΆλ ₯μμ ββκ΅¬μ± λ° μμ² κ°μ²΄λ₯Ό λ³Ό μ μμ΅λλ€. λλ λΉμ μ΄ κ·Έ μΆλ ₯μ νμΈν΄μΌνλ€κ³ μκ°ν©λλ€. μλ²κ° λ€μ 보λ΄λ μλ΅ νμκ³Ό κ΄λ ¨μ΄ μμ μ μμ΅λλ€.
@luxueyan Access-control-allow-origin
μ http://localhost:8081
λ‘ μ€μ ν κ² κ°μ΅λλ€(μ΄ μ£Όμκ° http://10.31.143.32:8080
μλ²μ μ€μ λμ΄ μλ€κ³ κ°μ ). URLμ΄ λ¬΄μμΈμ§ νμΈν μ μμ΅λκΉ? μ΄ νλ‘μ νΈλ₯Ό μ€ννκ³ μμ΅λκΉ? http://localhost:8081
μ
λκΉ? κ·Έλ μ§ μμΌλ©΄ ν¬νΈ λ²νΈκ° λ¬λΌλ CORS λλ©μΈ κ·μΉμ μλ°νλ κ²μ
λλ€.
@petruscurtu μλ΅ μΈν°μ ν°μμ μΆλ ₯μ 'JSON.parse(JSON.stringify(error))'μ λλ€. μλ΅ μμ±μ΄ μμ΅λλ€
μ λ°μ΄νΈ! κ·Έκ²μ μλ² API μ€λ₯μμ΅λλ€. ν€λμ cors μ€μ μ΄ μμμ΅λλ€. μ΄μ μ μλν©λλ€.
@juliano-barros κ°μ¬ν©λλ€. κ΅μ°¨ μΆμ² λλ¬Έμ΄μμ΅λλ€. μ΄μ λͺ¨λ ajax μμ²μ΄ μ λλ‘ μλν©λλ€.
μ΄ μμ μ€λ₯λ μλ²μ μ°κ²°ν μ μκ±°λ CORSκ° λ¬΄ν¨νλ λλ§λ€ λ°μν©λλ€.
error.response
λ undefined
μ
λλ€.
λ°©κΈ λ©μμ§, μν λ° μλ΅ λ°μ΄ν°λ₯Ό ν¬ν¨νμ§λ§ νμ λμΌν ꡬ쑰λ₯Ό λ°λ₯΄λ IntegrationErrorλ₯Ό μμ±νμ΅λλ€. μ΄λ κ² νλ©΄ μ²λ¦¬μμ axios/raw μ€λ₯λ₯Ό λΆλ¦¬ν μ μμ΅λλ€.
Axios μ¬λ¬λΆ, μ΄κ²μ error.response
ν¬ν¨νμ¬ HTTP μ€λ₯μ¬μΌ ν©λλ€.
μμ κ°μ΄ νμΈν μ μμ΅λλ€
μ΄μ λ μ΄ λ¬Έμ μ 1μ£Όλ μ΄ λλ λ μ΄μμ΅λλ€. μ΄ μμ μμ "κ³ μ "λκ±°λ "λΉ λ¬Έμ "λ‘ κ°μ£Όλ©λκΉ?
@frostshoxx κ·Έλμ λλ μ΄κ²μ΄ "λΉλ¬Έμ "λΌκ³ μκ°ν©λλ€. λ€λ₯Έ λͺ¨λ μ¬λλ€κ³Ό κ°μ λ¬Έμ λ₯Ό κ²ͺκ³ μμ§λ§ μ΄μ λ¬΄μ¨ μΌμ΄ μΌμ΄λκ³ μλμ§ μ΄ν΄νκ³ μ΄κ²μ΄ λ²κ·Έλ λ¬Έμ κ° μλλΌ μΈμμ΄ μλνλ λ°©μμ΄λΌλ κ²μ μ μ μμ΅λλ€.
νΌλμ€λ¬μνλ λͺ¨λ μ¬λλ€μ μν΄(κ·Έλ¦¬κ³ μ΄ λ¬Έμ κ° 1λ μ΄ λ κ²μ μΆννκΈ° μν΄) λ¬΄μ¨ μΌμ΄ μΌμ΄λκ³ μμΌλ©° μλ² μλ΅μ λ°λ λ°©λ²μ μ€λͺ νκ² μ΅λλ€.
.catch()
ν©λ¬΄μ§λ₯Ό λ§λλ λ°μλ μ¬λ¬ κ°μ§ μ΄μ κ° μμ΅λλ€. λλλ‘ Axiosλ μμ² μ΄κΈ°μ μ€λ₯κ° λ°μν©λλ€. μ΄κ²μ λλλ‘ CORS μμ²μμ λ°μν©λλ€. Axios OPTIONSλ₯Ό μ¬μ©ν μ μλ κ²½μ° Axiosλ ν¨λ μνλ‘ μλ²μμ μ€μ λ‘ μλ΅μ λ°κΈ° μ μ .catch()
λ‘ μ¬μ©μλ₯Ό 보λ΄λ―λ‘ μ΄λ¬ν κ²½μ° μλ΅μ μ¬μ©ν μ μμ΅λλ€.
μ¬λ¬λΆ μ€ μΌλΆλ "μλμ, DevToolsμμ κ·Έκ²μ 보μμΌλ―λ‘ μλ΅μ΄ λμμμ΅λλ€!"λΌκ³ λ§ν μ μμ΅λλ€. λλλ‘ Axiosκ° HTTP μμ²μ μμνκ³ μ€λ₯κ° λ°μνκ³ μ체λ₯Ό μ€λ¨νκ³ μλ²κ° μλ΅νκΈ° μ μ .catch()
보λ΄κΈ° λλ¬Έμ μ΄κ²μ΄ νμ μ¬μ€μΈ κ²μ μλλλ€. Axiosκ° .catch()
ν¨μλ₯Ό μ€νν ν λͺ λ°λ¦¬μ΄ νμ μλ²κ° μλ΅κ³Ό ν¨κ» λμμ¬ μ μμ΅λλ€. μ΄ μλ΅μ μ¬μ ν ββμλ΅μ μμ νκ³ μκΈ° λλ¬Έμ DevToolsμ νμλ©λλ€. κ·Έλ¬λ Axiosλ μ΄ μμ μμ μ€λ«λμ μνλμμΌλ―λ‘ Axiosκ° .catch()
κΈ°λ₯μ μμν λΉμμ μ‘΄μ¬νμ§ μμκΈ° λλ¬Έμ response
λ₯Ό μ¬μ©ν μ μμμ΅λλ€.
μλ²μμ 4XX λλ 5XX μ€λ₯μ λν λ©μμ§λ₯Ό λ°μ λΆλ€μ μ΄μ΄ μ’μ κ²μ
λλ€! μ΄ μ€λ₯λ₯Ό μ»κΈ° μν΄ Axiosλ μ 체 μλ² μλ΅μ κΈ°λ€λ €μΌ νκΈ° λλ¬Έμ μΌλ°μ μΌλ‘ μ¬μ©ν μ μλ μλ²μ μλ΅μ΄ μμ΅λλ€. μ΄λ¬ν κ²½μ° Axiosλ response
λ‘ μ¬μ©μλ₯Ό .catch()
λ‘ λ³΄λ΄μ§λ§ μλ΅μ μ¦μμμ μμ±νλ μ체 error
κ°μ²΄λ‘ λνν©λλ€.
λ°λΌμ 5XX λλ 4XX μ€λ₯κ° λ°μνλ κ²½μ° μλ² μλ΅μ μ¬μ©ν μ μμ§λ§ κΉμ΄ 묻ν μμ΅λλ€!
error.response.data.data.message
μλνμμμ€. μ΄κ²μ μλ² μ€λ₯λ₯Ό λ°ννλ Laravel API μμ²μ λν΄ μλνλ κ²μΌλ‘ νμΈλμμ΅λλ€.
λΉ Laravel μ¬μ©μ -- μλ²μμ ν΅μ¬ μλ΅μ μ»μΌλ €λ©΄ μ΅μν error.response.data
λ‘ μ΄λν΄μΌ νμ§λ§ λλΆλΆμ μλ²λ μλ΅μ λ€λ₯Έ data{}
κ°μ²΄λ‘ λννλ κ²λ³΄λ€ λ μ΄μ΄λ₯Ό λ κΉκ² ν λ€μ λ§μ§λ§μΌλ‘ κ±°κΈ°μμ .message
μ κ°μ μλ΅ μμ±μ μ‘μΈμ€ν©λλ€.
λ€, μμμ. νμ§λ§ κ·Έλ₯ μκΈ°λ €κ³ error.response.data
λ₯Ό μΆλ ₯ν΄ λ³΄μΈμ.
μ΄λ€ μ΄μ λ‘ Axiosλ μ€λ₯ κ°μ²΄μμ console.log(error)
μ μΆλ ₯μ μμ ν©λλ€. μ κ·Έλ€μ΄ μ΄κ²μ νλμ§λ λͺ¨λ₯΄κ² μ§λ§, λμ console.log(JSON.stringify(error))
λ₯Ό μ°λ©΄ μ 체 κ°μ²΄κ° μκ΄μΌλ‘ λ³΄μΌ κ²μ
λλ€.
μ΄κ²μ μ§λ ¬νλ JSONμΌλ‘ μ½ 2κ°μ λ μ΄μ΄ κΉμ΄ νμ μ½κΈ°κ° μ΄λ ΅μ΅λλ€. λ°λΌμ μ 체 λ΄μ©μ 볡μ¬νμ¬ JSON prettifyer μ λΆμ¬λ£μ΄ μ€λ₯ κ°μ²΄μ ꡬ쑰λ₯Ό νμν΄μΌ ν©λλ€.
Axiosμμ 500 μ€λ₯ λλ 400 μ ν μ€λ₯(λλ μ€μ λ‘ μλ²μμ μ 곡ν μ€λ₯ μ½λ)κ° λ°μνλ©΄ μμ²μ΄ μλ£λμμΌλ©° μλ²μ μλ΅μ μ¬μ©ν μ μμμ μλ―Έν©λλ€!
λ¬Έμ λ κ·Έκ²μ΄ λ§€μ° κΉμΌλ©° Axiosκ° λλΆλΆμ μ¬λλ€μ΄ error{}
κ°μ²΄λ₯Ό 보기 μν΄ μννλ console.log(error)
μ μΆλ ₯μ μμ νκΈ° λλ¬Έμ λλΆλΆμ μ¬λλ€μ κ·Έκ²μ΄ μ‘΄μ¬νλ€λ κ²μ λͺ¨λ¦
λλ€. νμ§λ§ κ±°κΈ°μ μμ΅λλ€!
@VSG24 맨!! μλͺ μ ꡬνλ λ΅λ³μ λλ€ :D
fwiw, μν μ°Έμ‘°λ‘ μΈν΄ 0.18.0μμ JSON.stringify(error)
ννμ΄ λ°μνμ΅λλ€.
μ€λ₯ λ°μ μ μλ² μλ΅μ μ 곡νλ μΈν°μ ν°λ₯Ό μ€μ νμ΅λλ€.
axios.interceptors.response.use(
response => {
// do someting on response
return response
},
error => {
// do someting on errir
return Promise.reject(error.response.data) // => gives me the server resonse
}
)
try {
const { data } = await htttp.post(login, payload)
// do someting with data
} catch (e) {
console.log(e) // server response data
}
@alimirayman λͺ¨λ μμ²μ μ£Όμν νμκ° μλ€λ©΄ μ μλνλ κ² κ°μ΅λλ€. κ·Έλ¬λ λͺ¨λ μμ²μ μΆμ ν΄μΌ νλ κ²½μ° λ΄ μ½λμ λͺ¨λ μμΉμμ μ€λ₯ μ²λ¦¬κΈ°λ₯Ό λ°λ³΅νλ κ²μ ν©λ¦¬μ μ΄μ§ μμ΅λλ€. λ΄κ° μλͺ» μ΄ν΄νμ§ μλ ν(κ²½μ°μ λ°λΌ λ€λ¦), axiosλ μ€λ₯λ₯Ό λμ§κΈ° μ μ μλ²κ° μ€λ₯λ₯Ό λ°ννλμ§ νμΈν΄μΌ νμ§ μκ² μ΅λκΉ?
@hhowe29 circular-json
λ€λ₯Έ λ§μ μ€λ λμ λ§μ°¬κ°μ§λ‘ μ΄ μ€λ λλ₯Ό μ°Ύμκ³ vue.js λ° vuex λͺ¨λκ³Ό ν¨κ» axiosλ₯Ό μ¬μ©νκ³ μμ΅λλ€.
λ΄ vuex λͺ¨λ μμ μ axios μ€λ₯λ₯Ό catchνκ³ commit(ERROR_HAPPENED, error)μ μ€ννμ¬ vuex λͺ¨λμ ERROR_HAPPENED λμ°λ³μ΄λ₯Ό μ€νν©λλ€. κ·Έλ¬λ μ€λ₯λ μλ΅ μμ± μμ΄ λμ°©νμ¬ vuex μνλ‘ μλ΅μ μ€μ νλ €κ³ νλ©΄ error.responseκ° μ μλμ§ μκ² λ©λλ€.
λ΄κ° console.log(error.response) μμ μ μννλ©΄ commit() μ μ μλ΅μ λ³Ό μ μμ΅λλ€. κ·Έλμ vuexμ μν΄ μλ§μ΄ λ κ°λ₯μ±μ΄ ν½λλ€.
commit(ERROR_HAPPENED, error.response.data) λλ μμ μμ 401μΈμ§ νμΈνκ³ μν μ½λμ λ°λΌ λ€λ₯Έ 컀λ°μ μ€νν©λλ€. μ΄ λ§ΉκΈλ§μ 401 μ½λμμλ§ λ°μνκΈ° λλ¬Έμ λλ€. 404 λλ 500κ³Ό κ°μ λ€λ₯Έ μ€λ₯λ λ΄ λμ°λ³μ΄λ‘ μ λ¬λ©λλ€.
λ§κ°μ§μ§ μμμ΅λλ€. Vuexλ λ²κ·Έκ° μλλΌλ μ΄κ²μ λ§ΉκΈλ§νμ§ μμ΅λλ€.
λλΆλΆ Axios μΈν°μ
ν° λ΄λΆμμ λ°μνλ μΌλΆ μ€λ₯λ‘ μΈν΄ μ΄κ²μ΄ λ
ΈμΆλμ§ μμ΅λλ€.
μΌλΆ μ λ°μ΄νΈ ?
@AllanPinheiroDeLima λ΅λ³μ΄ λ¦μ΄ μ£μ‘ν©λλ€. νμ§λ§ λκ΅°κ° μ½λμ λͺ¨λ λ¨μΌ μμΉμ μΈν°μ
ν°λ₯Ό μμ±νμ§ μλλ€κ³ μκ°ν©λλ€. μ°λ¦¬λ μ½λ λ² μ΄μ€μ μΈν°μ
ν°λ₯Ό ν λ² μμ±νκ³ μ΄λ₯Ό λμ°λ―Έ ν΄λμ€λ‘ μ¬μ©ν©λλ€. 1λ²μ
λλ€. 2λ², μ΄κΈ° 쿼리λ _server error response_λ₯Ό λ°μ§ λͺ»νλ€λ κ²μ
λλ€. λμ try catch
μμ _javascript error_λ₯Ό λ°μμ΅λλ€ axios
μ€λ₯ μΈν°μ
ν°λ κΈ°λ³Έμ μΌλ‘ μ½λ λ°©μμ λ³κ²½νκ±°λ λκ·λͺ¨ μ½λ κΈ°λ°μ λ³κ²½νμ§ μμΌλ €λ κ²½μ° ν΄λΉ λ¬Έμ λ₯Ό ν΄κ²°νκΈ° μν λ―Έλ€μ¨μ΄ μν μ ν©λλ€.
μ, λμν©λλ€. μμ keycloakμ μ¬μ©νλ λμ λ΄ μλ²μ ν΅ν©νλ μ΄ λ¬Έμ μ λν ν΄κ²°μ± μ μ°Ύμμ΅λλ€. μ΄λ κ² νλ©΄ μλ²μμ μ€λ₯λ₯Ό κ°λ‘μ±μ μ½μ μ μλ μ€λ₯λ₯Ό νλ°νΈ μλλ‘ λ³΄λΌ μ μμ΅λλ€. μ§κΈμΌλ‘μλ μ΄λ° μ’ λ₯μ λ¬Έμ λ₯Ό ν΄κ²°ν μ μλ μ μΌν λ°©λ²μ΄λΌκ³ μκ°ν©λλ€. :)
μμ§ κ°μ λ¬Έμ κ° μμ΅λλ€. Axios μΈν°μ
ν° console.log('interceptors', JSON.stringify(error))
νλ©΄ μ΄ λ‘κ·Έ interceptors {"config":{"transformRequest":{},"transformResponse":{},"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"},"method":"post","url":"http://localhost:5000/api/autenticacao/login","data":"{\"siglaInstituicao\":\"NEAS\",\"usuario\":\"emersoncpaz\",\"senha\":\"belle1903\"}"},"request":{}}
λμ§λ§ chrome devtools μλ΅μλ APIμμ μ¬λ°λ₯Έ μλ΅μ΄ μμ΅λλ€. A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
axiosμμ μ΄ μλ΅μ μ»λ λ°©λ²μ 무μμ
λκΉ?
5xx μ€λ₯κ° catch λΈλ‘μμ μ²λ¦¬λμ§ μλ μ΄μ κ° λ¬΄μμ λκΉ(μλ΅ κ°μ²΄λ₯Ό λ°ννμ§ μμ)? μν μ½λ 502λ DevTool μ½μμ λνλμ§λ§ Axiosμμ μ²λ¦¬λμ§ μμ΅λλ€.
μ΄κ²μ λλ₯Ό μν΄ μΌνμ΅λλ€ (λΌλΌλ²¨ μ€λ¨ λμ°λ―Έ μ¬μ©: abort(500, 'message'))
```
.catch(μ€λ₯ => {
console.log(error.response.data.message);
});
```
λꡬλ μ§ μ΄κ²μ΄ μλνλ€κ³ λ§νλ μ½λ μ‘°κ° μ΄μΈμ μμ ν μλ μμ κ° μμ΅λκΉ? μ΄ μ€λ λμμ κ·Έλ€μ΄ λꡬλ₯Ό μΈκΈνλμ§, μ½λ μ‘°κ°μ μ΄λμ λ£μλμ§, μ΅μ’ μμ μ½λκ° μ΄λ»κ² μκ²Όλμ§ λͺ¨λ₯Έλ€λ©΄ ν μ€μ μ‘°κ°μ λ³λ‘ λμμ΄ λμ§ μμ΅λλ€.
λλ μΈν°μ ν°μμ μ€λ₯λ₯Ό μ‘μΌλ €κ³ λ Έλ ₯νκ³ μμΌλ©° μ€λ₯ μ½λλ₯Ό κΈ°λ°μΌλ‘ 401μ΄ λ‘κ·ΈμΈνλ κ²½μ° λ¦¬λλ μ μΌλ‘ μμ²μ μ’ λ£νκ±°λ 422κ° μμ²μ νΈμΆ κ΅¬μ± μμλ‘ λμκ° μμ μ€λ₯ μ λ°μ΄νΈλ₯Ό νμ©νλλ‘ νμ©ν©λλ€.
μ΄λ€ μμ΄λμ΄?
λλ Nuxt axiosλ₯Ό μ¬μ©νκ³ μμΌλ©° onResponseError μΈν°μ ν°λ λ€μκ³Ό κ°μ΅λλ€.
$axios.onResponseError(error => {
const code = parseInt(error.response && error.response.status)
console.log(code, 'Axios onResponseError')
console.log(error.response.status, 'status')
console.log(error.config, 'config')
console.log(error.response, 'response')
// Do something with response error
if (error.response.status === 401) {
console.log('unauthorized, logging out ...')
app.$auth.logout()
redirect('/login')
}
if (code === 400) {
// redirect('/400')
console.log(`400 onResponseError`)
}
if (code === 408) {
console.log(`A timeout happened on url onResponseError`)
}
if (code === 418) {
console.log(`A teapot onResponseError`)
}
if (code === 428) {
console.log(`428 onResponseError`)
// redirect('/login')
}
return Promise.reject(error)
})
μν νΈμΆμ λ€μκ³Ό κ°μ΅λλ€.
async postRecord (clear = false) {
try {
const { r } = await this.$axios.$post(this.endPoint, this.formData)
console.log(r, 'response')
// do something with response
} catch (e) {
console.log(e, 'error in crud') // server response data
// i should never get here if error since the interceptor should kill the request if 401
// for testing this request is returning a 401
} finally {
this.submitted = false
}
},
κ°μ¬ ν΄μ,
λ°μ΄λΈ
μΈν°λ·μ΄ μμΌλ©΄ error.responseκ° μ μλμ§ μμ΅λλ€. μ΄κ²μ΄ μ΅μ μ λ°©λ²μ λκΉ?
μΌλ° ν μ€νΈμμ μ€λ₯κ° λ°μν©λλ€. κ°μ²΄μ μ€λ₯λ₯Ό λ³ννλ €λ©΄ λ€μμ μ¬μ©ν΄μΌ νμ΅λλ€.
var errorObj = JSON.parse(JSON.stringify(error));
μ΄κ²μ axiosκ° μλλΌ μλ²μ λ¬λ € μμ΅λλ€.
error.response.status, μ μλμ§ μμ κ²½μ° μΈν°λ·μμ μ μ μμ΅λλ€.
JSμ μ°κ²° λ¬Έμ λλ ꡬ문 μ€λ₯
2019λ
1μ 2μΌ μμμΌ, 15:40 Ibrahim Azhar Armar < [email protected]
μΌλ€:
μΌλ° ν μ€νΈλ‘ μ€λ₯κ° λ°μν©λλ€. μ€λ₯λ₯Ό λ³ννλ €λ©΄ λ€μμ μ¬μ©ν΄μΌ νμ΅λλ€.
κ°μ²΄.var errorObj = JSON.parse(JSON.stringify(μ€λ₯));
β
λΉμ μ΄ λκΈμ λ¬μκΈ° λλ¬Έμ μ΄κ²μ λ°λ κ²μ λλ€.
μ΄ μ΄λ©μΌμ μ§μ λ΅μ₯νκ³ GitHubμμ νμΈνμΈμ.
https://github.com/axios/axios/issues/960#issuecomment-450896476 λλ μμκ±°
μ€λ λ
https://github.com/notifications/unsubscribe-auth/AEETt2r3bt2QCixDlzh4fZvWp_1rPkxuks5u_NMEgaJpZM4N9Ljl
.
@kgrosvenor μλ² μ€ν¬λ¦½νΈλ₯Ό μ μ λͺ¨λλ‘ μ ννκ³
μ¬λ°λ₯Έ μ€λ₯ λ©μμ§κ° νμλμ§λ§ μ½μμ μΌλ° ν μ€νΈλ‘ νμλλ―λ‘ μλ²μ κ΄λ ¨μ΄ μλ νμ λ¬Έμ λ§ λ³΄μ λλ€.
κ·Έλ λ€λ©΄ μλ²μμ 무μμ΄ μ΄κ²μ μΌμΌν¬ μ μλμ§ μ΄ν΄νλ €κ³ λ Έλ ₯νκ³ μμ΅λκΉ?
μκ° μ΄κ³Όμ λλ¬νμ κ°λ₯μ±μ΄ ν½λλ€. μ€λ₯μ 'μλ΅'μ΄ μλμ§ νμΈνμμμ€.
axios.get()
.then(function(done) {
//fine and can get done.data
})
.catch(function(err) {
//catch the error, check it has a response object with lodash
if(_.has(err, 'response') {
console.log(error.response.status);
console.log(error.response.data);
}
else {
console.log("Most likely a server timeout or an internet connection error");
console.log("error response property is undefined")
}
});
Java νλ‘κ·Έλλ¨Έκ° 500 μ€λ₯κ° ν¬ν¨λ μ€λ₯ λ©μμ§λ₯Ό 보λ΄λλ° ν΄λΉ λ©μμ§λ₯Ό μ²λ¦¬ν μ μμ΅λλ€. κ·Έλμ λλ κ·Έμκ² μλ΅μΌλ‘ μ€λ₯ λ©μμ§μ ν¨κ» 200 μνλ₯Ό λ°ννλλ‘ μμ²νκ³ μμ§λ§ κ·Έλ λμκ² νλ₯Ό λ΄λ©° κ·Έμ HTTP ν΄λΌμ΄μΈνΈκ° 500 μνμ λͺ¨λ λ©μμ§λ₯Ό λ³Έλ€κ³ λ§ν©λλ€. κ·Έλ λ΄κ° 500 μνμ μ€λ₯ λ©μμ§λ₯Ό μ°Ύμ μ μκΈ° λλ¬Έμ λ΄κ° λ°λ³΄λΌκ³ μκ°ν©λλ€. axiosκ° 500 μ€λ₯ λ©μμ§λ₯Ό μ²λ¦¬νμ§ μλλ€λ κ²μ μ΄λ»κ² μ€λͺ ν©λκΉ?
Java νλ‘κ·Έλλ¨Έκ° 500 μ€λ₯κ° ν¬ν¨λ μ€λ₯ λ©μμ§λ₯Ό 보λ΄λλ° ν΄λΉ λ©μμ§λ₯Ό μ²λ¦¬ν μ μμ΅λλ€. κ·Έλμ λλ κ·Έμκ² μλ΅μΌλ‘ μ€λ₯ λ©μμ§μ ν¨κ» 200 μνλ₯Ό λ°ννλλ‘ μμ²νκ³ μμ§λ§ κ·Έλ λμκ² νλ₯Ό λ΄λ©° κ·Έμ HTTP ν΄λΌμ΄μΈνΈκ° 500 μνμ λͺ¨λ λ©μμ§λ₯Ό λ³Έλ€κ³ λ§ν©λλ€. κ·Έλ λ΄κ° 500 μνμ μ€λ₯ λ©μμ§λ₯Ό μ°Ύμ μ μκΈ° λλ¬Έμ λ΄κ° λ°λ³΄λΌκ³ μκ°ν©λλ€. axiosκ° 500 μ€λ₯ λ©μμ§λ₯Ό μ²λ¦¬νμ§ μλλ€λ κ²μ μ΄λ»κ² μ€λͺ ν©λκΉ?
μ€μ λ‘ μν 500μ μ€λ₯λ₯Ό μ²λ¦¬ν μ μμ΄μΌ νλ©° μλ΅μΌλ‘ http μνλ₯Ό νμΈνκ³ ν΄λΌμ΄μΈνΈ μΈ‘μμ μ²λ¦¬νλ λ°©λ²μ κ²°μ ν μ μμ΅λλ€. μ΄κ²μ μ΄λ―Έ μ΄ λ§ν¬(https://github.com/axios/axios#handling-errors)μ λ¬Έμνλμ΄ μμ΅λλ€.
λ€μμ μλ²μμ μ€λ₯ 500μ μ‘λ λ°©λ²μ΄λ©° μ μκ² ν¨κ³Όμ μ λλ€.
.catch(function (error) {
var errorObj = JSON.parse(JSON.stringify(error));
console.log(errorObj);
if (errorObj.code == 'ECONNABORTED') {
alert('Connection timed out.');
}
});
μκ΄μμ΄ 500 μνλ₯Ό μ²λ¦¬ν μ μμ§λ§ μ μκ°μλ μλ² μΈ‘μμ μλμ μΌλ‘ κ·Έκ²μ λ°ννλ κ²μ κ½€ λμ μ΅κ΄μ λλ€...
λν error.response undefinedκ° νμλκ³ μλ μ½λλ₯Ό μλν©λλ€.
axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
// Do something with response error
let {response} = error;
// response?.status === 401 es7 way.
if (response && response.status === 401) {
// Do logout;
// Redirect to login;
return Promise.reject(error.response);
}
else if (error.response){
// Do something.
console.log('some API_CLIENT error');
return Promise.reject(error.response);
}
return Promise.reject(error);
});
μ΄κ²μ λ무 λμ©λλ€. μ€λ₯ μ²λ¦¬κ° μ€μν©λλ€.
μ§κΈμ μ΄λ»κ² λκ³ μλμ? λ²μ 0.18.0μμ μ¬μ ν λμΌν λ¬Έμ κ° λ°μν©λλ€!
λμκ² κ°μ λ¬Έμ : @nuxtjs/axios v5.0.0
λ²κ·Έμ λλ€
Daniel Stern [email protected]δΊ 2019εΉ΄2ζ28ζ₯ε¨ε 04:39ειοΌ
μ΄ ν λ‘ μ μμ½νκ² μ΅λλ€.
λ°μ¬:
EVERYONE HTTP μ€λ₯λ₯Ό μ‘μ μ μλ€λ κ²μ΄ λ¬Έμ μ λλ€.
κ°λ°μ HTTP μ€λ₯λ₯Ό μ‘μ μ μλ κ²μ λ¬Έμ κ° μλλλ€ .β
λΉμ μ΄ λκΈμ λ¬μκΈ° λλ¬Έμ μ΄κ²μ λ°λ κ²μ λλ€.
μ΄ μ΄λ©μΌμ μ§μ λ΅μ₯νκ³ GitHubμμ νμΈνμΈμ.
https://github.com/axios/axios/issues/960#issuecomment-468020991 λλ μμκ±°
μ€λ λ
https://github.com/notifications/unsubscribe-auth/AAN8CbMHohyuSZHYlcieD_5yUqizpkSYks5vRuzygaJpZM4N9Ljl
.
READMEμμ μ΄ λ¬Έμ λ₯Ό μΈκΈν΄μΌ ν©λλ€. λ§€μ° λμμ΄ λ©λλ€. κ°μ¬ ν΄μ!
https://github.com/axios/axios/issues/960#issuecomment -320659373
λ€λ₯Έ ν¬μ€ν°μ λμν©λλ€. μ€λ₯κ° λ°μνλ μ§μ μ λ°λΌ catch
μ μμΈ‘ν μ μλ ꡬ쑰μ κ°μ²΄λ₯Ό λΆμ¬νλ κ²μ μ’μ§ μμ΅λλ€. error.response
λ° JSON.stringify(error)
λͺ¨λ μμΈμ λ°λΌ μ체μ μΌλ‘ μ€λ₯κ° λ°μν μ μμ΅λλ€. https://github.com/axios/axios#handling -errorsμμ μ μλ μ κ·Ό λ°©μλ μ΄μ λν΄ μ νν μλ €μ£Όμ§ μμ΅λλ€.
λμ°ν μ€λ₯ λ©μμ§λ₯Ό λ°ννκΈ° μν΄ μ΄ λͺ¨λ 곑μλ₯Ό μνν΄μΌ νλ κ²μ 2019λ μ λΉνΉμ€λ¬μ μ΅λλ€. 1990λ μλ λΉνΉμ€λ¬μ μ κ²μ λλ€. μ§μ§νκ² JS μ¬λλ€, κ²μμ κ°ννμμμ€. API μ€κ³λ₯Ό λ°°μ°κ³ μλμ μΌλ‘ 무μνκ³ μλ νΌλμ λλ²κΉ νλ λ° μμ² λͺ μ μ¬λλ€μ΄ μλ§μ μκ°μ μλΉνλ 'μ°½μμ μΈ κ²°μ ' λΉμ©μ μΈμνμμμ€.
μν μ°Έμ‘°λ‘ μ΄λ €μμ κ²ͺλ μ¬λλ€μ μν΄ λ€μκ³Ό κ°μ΄ ν μ μμ΅λλ€.
try {
// your axios request
} catch (error) {
const { response } = error;
const { request, ...errorObject } = response; // take everything but 'request'
res.status(response.status);
return res.json(errorObject); // or use 'return res.json(response.data.error);' then you don't need to omit the 'request'
}
μν μ°Έμ‘°λ‘ μ΄λ €μμ κ²ͺλ μ¬λλ€μ μν΄ λ€μκ³Ό κ°μ΄ ν μ μμ΅λλ€.
try { // your axios request } catch (error) { const { response } = error; const { request, ...errorObject } = response; // take everything but 'request' res.status(response.status); return res.json(errorObject); // or use 'return res.json(response.data.error);' then you don't need to omit the 'request' }
κ³ λ§μμ! κ·Έκ²μ λλ₯Ό μν΄ μ μλνλ μ½λμ λλ€.
λ¬Έμ λ "lib/core/enhanceError"μ μμ΅λλ€. μμ² κ°μ²΄κ° μ€λ₯μ μΆκ°λ©λλ€. Promise κΈ°λ° APIμλ νλμ 맀κ°λ³μλ§ μμΌλ―λ‘ λ΅λ³μ μλ΅μ μΆκ°νλ κ²μ΄ ν©λ¦¬μ μ
λλ€(μμ² λΌμ΄λΈλ¬λ¦¬λ 첫 λ²μ§Έ 맀κ°λ³μλ‘ μ€λ₯λ₯Ό λ°ννκ³ λ λ²μ§Έ 맀κ°λ³μλ‘ μλ΅μ λ°νν¨). κ·Έλ¬λ λͺ¨λ μμ²μ μΆκ°νλ κ²μ μλ―Έκ° μμ΅λλ€. μ½λ λ³κ²½μ λ§€μ° κ°λ¨ν©λλ€ https://github.com/geoblink/axios/pull/1/files
μ€μν΄ λ³΄μ΄λ κ²½μ° λ©μΈ λΈλμΉμ λν ν보λ₯Ό μ΄ μ μμ΅λλ€(λ¬Έμ κ° μ’
λ£λμκΈ° λλ¬Έμ μννμ§ μμ).
μμ²ν λλ§λ€ νΈμΆν ν¨μ λνΌλ₯Ό λ§λ€ μ μμ΅λλ€.
const api = async (uri, data) => {
try {
const { data: response } = await axios({
url: domain + uri,
method: 'POST',
data,
});
return response.data;
} catch (error) {
throw error.response.data;
}
};
μκ° μ΄κ³Όμ λλ¬νμ κ°λ₯μ±μ΄ ν½λλ€. μ€λ₯μ 'μλ΅'μ΄ μλμ§ νμΈνμμμ€.
axios.get() .then(function(done) { //fine and can get done.data }) .catch(function(err) { //catch the error, check it has a response object with lodash if(_.has(err, 'response') { console.log(error.response.status); console.log(error.response.data); } else { console.log("Most likely a server timeout or an internet connection error"); console.log("error response property is undefined") } });
μ΄κ²μ λλ₯Ό μν΄ μΌνμ΅λλ€. κ°μ¬ν©λλ€
"error.response"λ₯Ό μ¬μ©νλ©΄ μλνμ§ μμ΅λλ€. λ¬Έμ λ λ°μ΄ν°λ² μ΄μ€ μλ²μ μ°κ²°μ λκ³ μΉ μλ²μμ μ€λ₯ 500μ λ°ννμ λ λ°μν©λλ€.
κ°λ° λꡬμ λ€νΈμν¬ ν
μλ΅ μ½λ: 500
μλ΅ λ³Έλ¬Έ:{ "error": { "statusCode": 500, "name": "Error", "message": "read ETIMEDOUT", "code": "ETIMEDOUT", "errno": "ETIMEDOUT", "syscall": "read", "stack": "Error: read ETIMEDOUT\n at exports._errnoException (util.js:1050:11)\n at TCP.onread (net.js:582:26)" } }
ν¬λ‘¬ μ½μ μ€λ₯:
Uncaught (in promise) Error: Request failed with status code 500 at createError (createError.js:15) at settle (settle.js:18) at XMLHttpRequest.handleLoad (xhr.js:77)
λ²μ
"axios": "^0.15.3"
μ¬κΈ°λ λ§μ°¬κ°μ§...
λ΄ κ²½νμ:
"catchμμ console.log(error)μμ console.log(error.response)λ‘ μμ ν©λλ€."
catchμμ console.log(error.response.data)μ ν¨κ» λ μ μλνμ΅λλ€.
axios.interceptors.response.use(null, μ€λ₯ => {
λ°ν error.response;
});
getClients: λΉλκΈ°(μν) => {
const res = axios.get( ${BASE_API}/clients
)μ κΈ°λ€λ¦½λλ€.
console.log('res',res);
λ§μ½ (res){
state.commit("setClient", res.data);
}
}
λ€λ₯Έ μ΅μ
/**
* Whenever error.message is accessed we want to se the full API error message (JSON) if it's present
* not just some generic http status code + message
* see https://github.com/axios/axios/issues/960 for context
*
* <strong i="6">@param</strong> axios
*/
export function extractAPIErrorResponse(axios: AxiosInstance) {
axios.interceptors.response.use(undefined, function (error: AxiosError) {
(error as any).originalMessage = error.message;
Object.defineProperty(error, "message", { get: function () {
if (!error.response) {
return (error as any).originalMessage;
}
return JSON.stringify(error.response.data);
}});
return Promise.reject(error);
})
}
λ
Έλ ₯νλ€{
κΈ°λ€λ¦¬μΈμ.$axios .....
}μΊμΉ(e)
{
console.log(e)
}
axios.get("https://......")
.then( response => {
return response
}).catch( () => {
store.dispatch("errorComponentMethod") // if there's an error, method is dispatched
})
//////////////////////////////////////
actions : {
errorComponentMethod(){
router.push("/errorComponent") // method routes to error component
}
}
κ°μ¬ν©λλ€! λ§μ λμμ΄ λμμ΅λλ€!
μ¬μ νμ΄ λ¬Έμ κ° λ°μν©λλ€. ν¬λ‘¬ λλ²κ±°μμ μλ΅μ μ ννμ§λ§ axiosλ λμ μΌλ° "Request failed with status code 403"
λ₯Ό λ°νν©λλ€.
λλ μμ§λ μ΄κ²μ κ°μ§κ³ μκ³ λλ μ λ§λ‘ νΌλμ€λ½λ€. μμ μ£Όμμ μ‘μΌλ €κ³ νμ§λ§ μ€λ₯κ° μμ λ μ νν axiosκ° undefinedλ₯Ό λ°ννλ μ΄μ λ 무μμ
λκΉ?
try{
const res = await axios.get("someurl");
return res;
} catch(e) {
console.log(e);
}
μλ²κ° μλ΅νμ§ μλ μ΄μ μ λν λ©μμ§κ° ν¬ν¨λ μλ΅μ λ€μ 보λ΄λλΌλ resλ μ μλμ§ μμ΅λλ€. κ·Έλ¦¬κ³ λλ κ·Έ λ°μ΄ν°μ μ κ·Όν μ μλ€... μ??
**νΈμ§: λ΄ λ²κ·Έλ₯Ό μ°Ύμμ΅λλ€ - axiosμ λν΄μλ μλ¬΄λ° λ¬Έμ κ° μμ΅λλ€. λ΄ μ견 λ κ° μλλ‘ μ°Έμ‘°νμμμ€. μ컨λ λ΄ μΈν°μ ν°λ axios νμ΄νλΌμΈμ ν΅ν΄ μ€λ₯ λ°μ΄ν°λ₯Ό λ€μ μ λ¬νμ§ μμμΌλ―λ‘ νμ μ μλμ§ μμμ΅λλ€.
@HoogsterInc μ¬κΈ° μμ
λΉμ μ μ΄λ° μΌμ ν μ μμ΅λλ€
try {
axios.get(...)
} catch (error) {
if (error.reponse) {
// http status 4x, 5xx
} else {
// network error like timeout, connection refused etc...
}
}
@konstantinblaesi λ₯Ό λκΈ° μν μλ΅μ κ°μ¬λ립λλ€ -- μ λ¬Έμ λ₯Ό νμ νμ΅λλ€.
λλ₯Ό μν΄ λλ μ¬μ©μμ μΈμ¦μ νμΈνκΈ° μν΄ μΈν°μ ν°λ₯Ό μ€μ νλλ°, μ¬μ©μκ° μΈμ¦λμ§ μμ κ²½μ° νΉμ μ€λ₯ μ½λλ₯Ό 보λ λλ€. κ·Έλ¬λ μ€λ₯κ° μΈμ¦λμ§ μμ μ½λμ μΌμΉνμ§ μμ λ μ€λ₯ λ°μ΄ν°λ₯Ό μ λ¬νμ§ μμμ΅λλ€. νμ¨... μ΄λ¦¬μμ λμ μ€μ. κ·Έλμ μΈμ¦λμ§ μμ μ€λ₯ μ½λλ₯Ό νμΈν ν Promise.reject(error); - λ΄ λ¬Έμ κ° ν΄κ²°λμκ³ μ€λ₯ λ°μ΄ν°κ° μ΄μ κ°λ μ°Όμ΅λλ€.
err.toJSON()
μ¬μ©ν μ μμ΅λλ€.
catch((err) => {
console.log(err.toJSON()); // Return error object
});
(κΈλ‘λ²λ‘ μ€μ )
main.jsμμ
import axios from 'axios'
var instance = axios.create(
{ validateStatus: function (status) {
return status < 600; #you can change this with another value
}
});
λλ
(νΉμ νκ² μ€μ )
axios.post('/formulas/create', {
name: "",
parts: ""
}, { validateStatus: function (status) {
return status < 600; // Reject only if the status code is greater than or equal to 500
}})
.then(
(response) => { console.log(response) },
(error) => { console.log(error) }
);
const log = console.log;
updateData(options = {}, flag = false){
let url = `/opapi/appDownloadGuide/update`;
let message = 'ζ΄ζ°ζε!';
let errorMessage = 'ζ΄ζ°ε€±θ΄₯!';
if (flag) {
message = 'ζ·»ε ζε!';
errorMessage = 'ζ·»ε ε€±θ΄₯!';
url = `/opapi/appDownloadGuide/add`;
}
axios
.post(url, options)
.then(res => {
// βπ€οΈ500 , ε¨θΏιζ¦δΈδ½εοΌ
log(`res`, res, res.status);
return res.data;
})
.then(json => {
const {
code,
success,
data,
errorCode,
errorHint,
} = json;
if(code === 200) {
this.$message({
type: 'success',
message,
});
this.init();
} else{
this.$message({
type: 'error',
message: `${errorMessage}: ${errorHint ? errorHint : ""}`,
});
}
})
.catch(err => {
// πε¨θΏιζ¦ζͺ error data!
log(`error.response`, err.response);
const {
data,
status,
statusText,
} = err.response;
this.$message({
type: "error",
message: `${statusText}-${status}: ${data || ""}`,
// message: "δΈθ½ιε€εε»Ί",
});
console.error(`500 err`, err);
});
},
λλ μ νν κ°μ νκ²½μ κ°μ§κ³ μλ€. μ΄ μλ:
axios.post('/formulas/create', { name: "", parts: "" }) .then(response => { console.log(response) }) .catch(error => { console.log(error.response) });
μμ _Modify
console.log(error)
μconsole.log(error.response)
μμcatch
_.μ μ μΈν°μ ν°λ₯Ό μ¬μ©νκ³
error.response
λ§ κ±°λΆν μλ μμ΅λλ€. λ¬Έμ λconsole.log
κ° μ€λ₯λ₯Ό μΆλ ₯νλ €κ³ ν λ κ°μ²΄ κ΅¬μ‘°κ° μλ λ¬Έμμ΄ ννμ΄ μΈμλμ΄.response
μμ±μ΄ νμλμ§ μλλ€λ κ²μ λλ€.
λλ₯Ό μν΄ μΌνμ§ μλλ€
@gopal-g μ μκ² μ μλμ§ μμμ΅λλ€.
@gopal-g μ λμΌν μ μλμ§ μμ λ©μ μ κ° κ°μ Έμ΅λλ€.
μ λ Axios 0.19.2λ₯Ό μ¬μ©νκ³ μμ΅λλ€. @konstantinblaesi μ μ루μ μ μΆκ°
λ€λ₯Έ μ΅μ
/** * Whenever error.message is accessed we want to se the full API error message (JSON) if it's present * not just some generic http status code + message * see https://github.com/axios/axios/issues/960 for context * * <strong i="9">@param</strong> axios */ export function extractAPIErrorResponse(axios: AxiosInstance) { axios.interceptors.response.use(undefined, function (error: AxiosError) { (error as any).originalMessage = error.message; Object.defineProperty(error, "message", { get: function () { if (!error.response) { return (error as any).originalMessage; } return JSON.stringify(error.response.data); }}); return Promise.reject(error); }) }
μ΄κ²μ μ μκ² ν¨κ³Όμ μ΄μμ΅λλ€ .
λΆλͺ ν νμλ©΄ λ΄ μ±μμ λ΄ μλ²κ° 400 μ€λ₯λ₯Ό λ°νν©λλ€(μλ΅ν΄μΌ ν¨).
% curl -X POST http://my-server/api -d '{"foo": "bar"}'
{"status": 400, "message": "missing param XYZ"}
μ΄ μμ μ¬νμ΄ μμΌλ©΄ Axiosμμ name
, stack
, config
(μλ μμ²μ΄ μμ)λ§ ν¬ν¨νκ³ "μλ΅"μ΄ μλ μ€λ₯ κ°μ²΄λ₯Ό μ»μ΅λλ€. κ·Έλ¬λ μ΄ μμ μΌλ‘ μ€λ₯μλ μλ²μ μ¬λ°λ₯Έ message
μμ±μ΄ ν¬ν¨λ©λλ€.
μ΄ μμ μ¬νμ΄ μμΌλ©΄ "response" μμ±μ΄ μλ μ΄μ λ 1171λ²μ§Έ μ€μ μλ axios.jsμ settle
μ κ΄λ ¨λ κ² κ°μ΅λλ€.
module.exports = function settle(resolve, reject, response) {
var validateStatus = response.config.validateStatus;
if (!validateStatus || validateStatus(response.status)) {
resolve(response);
} else {
reject(createError(
'Request failed with status code ' + response.status,
response.config,
null,
response.request,
response
));
}
};
μ€λ₯λ₯Ό μμ±ν λ response.message
ν΅κ³Όνμ§ μλ κ²μ λ³Ό μ μμ΅λλ€. λν λ΄ μ±μμ error.response
μλ΅μ΄ νμλμ§ μμ΅λλ€. κ·Έλ° μμ±μ΄ μμ΅λλ€. μ΄ν΄κ° λμ§ μμ΅λλ€. κ·Έλ¬λ μ΄μ¨λ μμ μμ μ¬νμ μ μκ² ν¨κ³Όμ μ
λλ€.
μλ²μμ μ ν¨μ± κ²μ¬ μ€λ₯λ₯Ό μ‘μΌλ €κ³ ν©λλ€.
μνΈ:
axios.post('/formulas/create', { name: "", parts: "" }) .then( (response) => { console.log(response) }, (error) => { console.log(error) } );
μ½μ λ‘κ·Έ μΆλ ₯
Error: Request failed with status code 422 at createError (app.js:6765) at settle (app.js:29489) at XMLHttpRequest.handleLoad (app.js:6600)
λ€νΈμν¬ ν μΆλ ₯
{"name":["μ΄λ¦ νλλ νμμ λλ€."],"parts":["λΆν νλλ νμμ λλ€."]}λ΄ λ€νΈμν¬ νμ μλ΅μΈ JSON μμ μ ν¨μ± κ²μ¬κ° ν¬ν¨λ κ°μ²΄κ° νμλμ΄μΌ ν©λλ€. JS catch μΆλ ₯μ μ»λ κ² κ°μ΅λκΉ?
λν λ€μμ μλνμ΅λλ€.
axios.post('/formulas/create', { name: "", parts: "" }) .then(response => { console.log(response) }) .catch(error => { console.log(error) });
κ°μ κ²°κ³Ό
μ¬κΈ° λμ κ°μ νκ²½μ μ¬μ©νλ©΄μ λ λ§μ μ¬λλ€μ΄ κ°μ λ¬Έμ λ₯Ό κ²ͺκ³ μλ κ² κ°λ€.
https://laracasts.com/discuss/channels/vue/issues-with-axios-catch-method
- Axios λ²μ : ^0.16.2
- λ·°JS 2.3.4
- Vue ν νλ¦Ώ μ»΄νμΌλ¬ 2.3.4
- λΌλΌλ²¨ λ―Ήμ€
- νκ²½: λ Έλ v6.4.0, ν¬λ‘¬ 58, Mac OSX 10.12.4
axios.post('/곡μ/λ§λ€κΈ°', {
μ΄λ¦: "",
λΆμ: ""
})
.κ·Έ λ€μμ(
(μλ΅) => { console.log(μλ΅) },
).catch(err => consol.log(err.response.data)});
error.responseλ κ°μ²΄μ΄λ©° μ½μ λ‘κ·Έμ [κ°μ²΄,κ°μ²΄]λ₯Ό μΆλ ₯ν©λλ€.
μ€λ₯ λ©μμ§μ μ‘μΈμ€νλ €λ©΄ error.reposne.dataλ₯Ό μ¬μ©ν΄μΌ ν©λλ€.
μ°Έμ‘° : https://itnext.io/javascript-error-handling-from-express-js-to-react-810deb5e5e28
@chandukasm μ, νμ§λ§ μ κ²½μ°μλ error.response
_μμ΅λλ€_ -- μμ²μ΄ μ μ‘λκ³ μλ΅μ΄ λͺ
μμ μΌλ‘ μμ λλλΌλ μ μλμ§ μμ΅λλ€. λν μμ λ΄ μ견μ μ°Έμ‘°νμμμ€. μλ΅μ΄ μ‘΄μ¬νλλΌλ λ©μμ§λ 무μλ©λλ€(μΌλ° λ©μμ§λ‘ λ체λ¨).
error.responseλ κ°μ²΄μ΄λ©° μ½μ λ‘κ·Έμ [κ°μ²΄,κ°μ²΄]λ₯Ό μΆλ ₯ν©λλ€.
μ€λ₯ λ©μμ§μ μ‘μΈμ€νλ €λ©΄ error.reposne.dataλ₯Ό μ¬μ©ν΄μΌ ν©λλ€.
μ°Έμ‘° : https://itnext.io/javascript-error-handling-from-express-js-to-react-810deb5e5e28
console.logμ [object,object]κ° λ³΄μ΄λ©΄ JSON.stringify(error.response)λ₯Ό μ€ννμ¬ μ€μ μ€λ₯λ₯Ό νμΈνμμμ€.
error.response
κ°μ²΄κ° μμ΅λλ€.
return this.$axios
.post(postUrl, payload, { responseType: 'json' })
.then(
(response) => {
this.message = `RESP: ${JSON.stringify(response)}`;
},
(reason) => {
this.message = `REAS: ${JSON.stringify(reason)}`;
}
)
.catch((err) => {
this.message = `CATCH: ${JSON.stringify(err)}`;
});
νμ΄λ‘λμλ HTTP 422λ₯Ό νΈλ¦¬κ±°νκΈ° μν΄ μλμ μΌλ‘ μλͺ»λ κ°μ΄ ν¬ν¨λμ΄ μμ΅λλ€. μ΄ κ°μ Postmanμμ μμλλ‘ μλνκ³ μλ²(LB4)κ° μ λ ₯ λ°μ΄ν°κ° μλͺ»λ μμΉλ₯Ό μ€λͺ νλ JSON νμμ μμ ν μ€λ₯λ₯Ό λ°ννμ§λ§ axiosμμλ error.response κ°μ²΄κ° μ μλμ§ μμ΅λλ€. .
κ°μ₯ μ μ©ν λκΈ
λλ μ νν κ°μ νκ²½μ κ°μ§κ³ μλ€. μ΄ μλ:
μμ μμ
console.log(error)
μconsole.log(error.response)
μμcatch
.μ μ μΈν°μ ν°λ₯Ό μ¬μ©νκ³
error.response
λ§ κ±°λΆν μλ μμ΅λλ€. λ¬Έμ λconsole.log
κ° μ€λ₯λ₯Ό μΆλ ₯νλ €κ³ ν λ κ°μ²΄ κ΅¬μ‘°κ° μλ λ¬Έμμ΄ ννμ΄ μΈμλμ΄.response
μμ±μ΄ νμλμ§ μλλ€λ κ²μ λλ€.