-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
axios module: failed response resolves promise #87
Comments
I was able to confirm that the proper response was returned when I bypass the proxy |
Hey. Are you testing with latest master version? What if you manually remove errorhandler's logic? |
Please upgrade to |
Also i think this would be correct usage: let user = (await app.$axios.get('/user/me')).data |
awesome. that did fix the issue! However, the caught error is now,
|
Would you please send a copy of generated |
import Axios from 'axios'
import Vue from 'vue'
const axiosPlugin = {
install() {
if(Vue.__nuxt_axios_installed__) {
return
}
Vue.__nuxt_axios_installed__ = true
if (!Vue.prototype.hasOwnProperty('$axios')) {
Object.defineProperty(Vue.prototype, '$axios', {
get () {
return this.$root.$options.$axios
}
})
}
// Vue Component Mixins
Vue.mixin({
methods: {
// opts
$request (opts) {
return this.$axios.request(opts);
},
// url, opts
$get (url, opts) {
return this.$axios.get(url, opts);
},
$delete (url, opts) {
return this.$axios.delete(url, opts);
},
$head (url, opts) {
return this.$axios.head(url, opts);
},
// url, data, opts
$post (url, data, opts) {
return this.$axios.post(url, data, opts);
},
$put (url, data, opts) {
return this.$axios.put(url, data, opts);
},
$patch (url, data, opts) {
return this.$axios.patch(url, data, opts);
}
}
})
}
}
Vue.use(axiosPlugin)
// Sets a common header
function setHeader (name, value, scopes = 'common') {
if(!Array.isArray(scopes)) {
scopes = [scopes]
}
scopes.forEach(scope => {
if (!value) {
delete this.defaults.headers[scope][name];
return
}
this.defaults.headers[scope][name] = value
})
}
// Set requests token
function setToken (token, type, scopes = 'common') {
const value = !token ? null : (type ? type + ' ' : '') + token
this.setHeader('Authorization', value, scopes)
}
const redirectError = {}
// Set appreciate `statusCode` and `message` to error instance
function errorHandler(error) {
if (error.response) {
// Error from backend (non 2xx status code)
// ...Auto redirect on special status codes
if (redirectError[error.response.status]) {
this.redirect(redirectError[error.response.status])
}
error.statusCode = error.statusCode || parseInt(error.response.status) || 500
error.message = error.message || error.response.statusText || (error.statusCode + ' (Internal Server Error)')
} else if (error.request) {
// Error while making request
error.statusCode = error.statusCode || 500
error.message = error.message || 'request error'
} else {
// Something happened in setting up the request that triggered an Error
error.statusCode = 500
error.message = error.message || 'axios error'
}
return Promise.reject(error)
}
// Setup BaseURL
const baseURL = process.browser
? (process.env.API_URL_BROWSER || '/api')
: (process.env.API_URL || 'http://667837fe8085:3000/api')
export default (ctx) => {
const { app, store, req } = ctx
// Default headers
const defaultHeaders = (req && req.headers) ? Object.assign({}, req.headers) : {}
delete defaultHeaders.host
// Create new axios instance
const axios = Axios.create({
baseURL,
defaultHeaders,
})
// Send credentials only to relative and API Backend requests
axios.interceptors.request.use(config => {
if (config.withCredentials === undefined) {
if (!/^https?:\/\//i.test(config.url) || config.url.indexOf(baseURL) === 0) {
config.withCredentials = true
}
}
return config
});
// Error handler
axios.interceptors.response.use(undefined, errorHandler.bind(ctx));
// Make accessible using *.$axios
app.$axios = axios
ctx.$axios = axios
if (store) {
store.$axios = axios
}
// Token helper for authentication
axios.setToken = setToken.bind(axios)
axios.setHeader = setHeader.bind(axios)
} |
I tought problem would be with new if (redirectError[error.response.status]) {
this.redirect(redirectError[error.response.status])
} |
where should i put this? |
This line is in |
i think that fixed it. now i see the response object. |
@uptownhr Sorry bothering, but would you please restart dev server if error exists? I can't reproduce it. |
@pi0 you're right. I restarted and still fixed. Sorry, should have rebuilt from scratch after upgrading. |
Failed response through the proxy resolves instead of a reject when ran from the server.
When returning statuscode 500 with message 'invalid token' from the API, the code below results in
user
beingExpected is that the statusCode is still 500 but
Request failed
frominvalid token
Request Code
The text was updated successfully, but these errors were encountered: