Skip to content

Commit 803baf9

Browse files
committed
catch XMLHttpRequest connection errors
1 parent e731c40 commit 803baf9

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ test/e2e/reports
77
selenium-debug.log
88
.idea
99
vanilla
10+
test-live.html

src/main.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ const ConfiguredVueRouter = config => new VueRouter({
3232
module.exports = function (config) {
3333
config = config || {}
3434

35-
if (config.root_url === undefined) {
36-
config.root_url = process.env.SOCKET_API_URL
35+
if (config.api_root === undefined) {
36+
config.api_root = process.env.SOCKET_API_URL
3737
}
3838

3939
if (config.element === undefined) {
@@ -72,7 +72,7 @@ module.exports = function (config) {
7272
// get_data is called by components, eg. grid
7373
get_data: function () {
7474
let xhr = new window.XMLHttpRequest()
75-
let url = config.root_url + '/contractors.json'
75+
let url = config.api_root + '/contractors.json'
7676
xhr.open('GET', url)
7777
xhr.onload = () => {
7878
let contractors
@@ -91,12 +91,19 @@ module.exports = function (config) {
9191
${e.toString()}
9292
requested url: "${url}"
9393
response status: ${xhr.status}
94-
9594
response text:
9695
${xhr.responseText}`
9796
Raven.captureException(new Error(this.error))
9897
}
9998
}
99+
xhr.onerror = () => {
100+
this.error = `\
101+
Connection error
102+
requested url: "${url}"
103+
response status: ${xhr.status}
104+
response text: "${xhr.responseText}"`
105+
Raven.captureException(new Error(this.error))
106+
}
100107
xhr.send()
101108
}
102109
}

test/unit/specs/main.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,26 @@ describe('main.vue', () => {
7777
setTimeout(() => {
7878
!expect(vm.error).to.not.equal(null)
7979
expect(vm.error).to.contain('Error: bad response 404')
80+
expect(vm.error).to.contain('response status: 404')
8081
expect(vm.error).to.contain('response text:\nbadness')
82+
expect(vm.error).to.not.contain('Connection error')
8183
done()
8284
}, 50)
8385
})
8486
})
8587

88+
describe('main.vue', () => {
89+
it('should show connection error', done => {
90+
let el = document.createElement('div')
91+
el.setAttribute('id', 'socket')
92+
document.body.appendChild(el)
93+
94+
const vm = socket({api_root: 'http://localhost:12345678'})
95+
96+
setTimeout(() => {
97+
expect(vm.error).to.contain('Connection error')
98+
expect(vm.error).to.contain('response status: 0')
99+
done()
100+
}, 50)
101+
})
102+
})

0 commit comments

Comments
 (0)