Skip to content

Commit

Permalink
Propagate errors in the multiplexer (yocontra#57)
Browse files Browse the repository at this point in the history
* propagate errors in the multiplexer

* do not fail the test when GDAL does not open the file

* update the changelog

* propagate errors in calcAsync

* fix the codecov YAML syntax

* the the progress catch
  • Loading branch information
mmomtchev authored Jan 3, 2023
1 parent bb67eba commit 1eb66a9
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.6.2] 2023-01-03

### Changed
- Fix [#56](https://github.com/mmomtchev/node-gdal-async/issues/56), propagate input errors in `calcAsync` and `RasterMuxStream`

## [3.6.1] 2022-12-21

### Added
Expand Down
4 changes: 3 additions & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ coverage:
- "src"
- "lib"
patch:
default: 80%
default:
target: auto
threshold: 2%
6 changes: 5 additions & 1 deletion lib/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ const calc = (gdal) => function calcAsync(inputs, output, fn, options) {
mux.on('data', (chunk) => {
processed += chunk[Object.keys(chunk)[0]].length
const done = processed / length
progress(done)
try {
progress(done)
} catch (e) {
reject(e)
}
})
}

Expand Down
8 changes: 8 additions & 0 deletions lib/multiplexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class RasterMuxStream extends Readable {
this.endHandlers = {}
this.blockOptimize = (options || {}).blockOptimize
this.rasterHighWaterMark = Infinity

for (const id of this.ids) {
const inp = inputs[id]
if (!(inp instanceof Readable)) throw new TypeError('inputs must be a map of Readables')
Expand All @@ -80,6 +81,8 @@ class RasterMuxStream extends Readable {

this.endHandlers[id] = this.inputEnded.bind(this, id)
this.inputs[id].on('end', this.endHandlers[id])

this.inputs[id].on('error', this.errorHandler.bind(this))
}
}

Expand Down Expand Up @@ -194,6 +197,11 @@ class RasterMuxStream extends Readable {
this.tryEnd()
}

errorHandler(e) {
debug('emitting error', e)
this.destroy(e)
}

_read() {
debug('readStart')
this.throttle(true)
Expand Down
45 changes: 45 additions & 0 deletions test/api_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,51 @@ describe('gdal_utils', () => {
)
})

it('should reject when the file is corrupted', function () {
const tempFile = `/vsimem/invalid_calc_${String(Math.random()).substring(2)}.tiff`
let T2m, D2m
try {
T2m = gdal.open(path.resolve(__dirname, 'data','AROME_T2m_10.tiff'))
D2m = gdal.open(path.resolve(__dirname, 'data','truncated.tiff'))
} catch (e) {
// Older GDAL versions cannot open the truncated file, so this is
// considered a successful test too
this.skip()
}
const size = T2m.rasterSize
const espyFn = (t: number, td: number) => 125 * (t - td)
return assert.isRejected(
gdal.calcAsync({
A: T2m.bands.get(1),
B: D2m.bands.get(1)
},
gdal.open(tempFile, 'w', 'GTiff', size.x, size.y, 1, gdal.GDT_Float64).bands.get(1),
espyFn),
/Cannot read/
)
})

it('should reject on progress exception', () => {
const tempFile = `/vsimem/invalid_calc_${String(Math.random()).substring(2)}.tiff`
const T2m = gdal.open(path.resolve(__dirname, 'data','AROME_T2m_10.tiff'))
const D2m = gdal.open(path.resolve(__dirname, 'data','AROME_D2m_10.tiff'))
const size = T2m.rasterSize
const espyFn = (t: number, td: number) => 125 * (t - td)
return assert.isRejected(
gdal.calcAsync({
A: T2m.bands.get(1),
B: D2m.bands.get(1)
},
gdal.open(tempFile, 'w', 'GTiff', size.x, size.y, 1, gdal.GDT_Float64).bands.get(1),
espyFn,
{ progress_cb: () => {
throw new Error('progress error')
} }
),
/progress error/
)
})

it('should reject when raster sizes do not match', () => {
const tempFile = `/vsimem/invalid_calc_${String(Math.random()).substring(2)}.tiff`
const espyFn = (t: number, td: number) => 125 * (t - td)
Expand Down
Binary file added test/data/truncated.tiff
Binary file not shown.

0 comments on commit 1eb66a9

Please sign in to comment.