diff --git a/lib/GtfsIndex.js b/lib/GtfsIndex.js index 710b595..d704ee7 100644 --- a/lib/GtfsIndex.js +++ b/lib/GtfsIndex.js @@ -48,12 +48,17 @@ class GtfsIndex { } async download(url, headers) { - const res = await request(url, { method: 'GET', headers }); + try { + const res = await request(url, { method: 'GET', headers }); - if (res.statusCode === 200) { - await this.unzip(await res.body); - } else { - throw new Error(`Error on HTTP request: ${url}, Message: ${await res.body.text()}`); + if (res.statusCode === 200) { + await this.unzip(await res.body); + } else { + throw new Error(`Error on HTTP request: ${url}, Message: ${await res.body.text()}`); + } + } catch (err) { + await this.cleanUp(); + throw err; } } @@ -295,7 +300,7 @@ class GtfsIndex { async cleanUp() { // We don't want to delete sources that are probably been reused - if(this.auxPath !== this.path) { + if (this.auxPath !== this.path) { await del([this.auxPath], { force: true }); } } diff --git a/lib/Utils.js b/lib/Utils.js index 0af5270..80eb4c4 100644 --- a/lib/Utils.js +++ b/lib/Utils.js @@ -9,7 +9,6 @@ function unzipStream(stream, outPath) { } stream.pipe(unzip.Extract({ path: outPath })) .on('error', async err => { - await this.cleanUp(); reject(err); }) .on('close', () => { diff --git a/package-lock.json b/package-lock.json index a07e813..af76f84 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "gtfsrt2lc", - "version": "2.0.4", + "version": "2.0.5", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "gtfsrt2lc", - "version": "2.0.4", + "version": "2.0.5", "license": "MIT", "dependencies": { "commander": "^5.0.0", diff --git a/package.json b/package.json index c157a4d..24c1717 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gtfsrt2lc", - "version": "2.0.4", + "version": "2.0.5", "description": "Converts the GTFS-RT to Linked Connections", "main": "./Gtfsrt2LC.js", "bin": { diff --git a/test/gtfsrt2lc.test.js b/test/gtfsrt2lc.test.js index fd62951..c2c0a2e 100644 --- a/test/gtfsrt2lc.test.js +++ b/test/gtfsrt2lc.test.js @@ -583,6 +583,10 @@ test('Cover GtfsIndex functions', async () => { try { await gti.getIndexes(); } catch (err) { } + + try { + await gti.download('http://google.com'); + } catch (err) { } }); test('Cover Utils functions', async () => {