Skip to content

Commit

Permalink
Improve error messages
Browse files Browse the repository at this point in the history
Lazy evaluation has made it more difficult to track down the source of
errors because all operations on a dataset are chained together and
executed at once. This adds a prefix to any errors emitted by GDAL that
explains where the error occurred.
  • Loading branch information
ddohler committed Oct 15, 2020
1 parent c61bdd4 commit 679a56a
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 22 deletions.
5 changes: 3 additions & 2 deletions src/wrappers/gdalClose.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ export default function (GDALClose, errorHandling) {
let errorType = errorHandling.CPLGetLastErrorType();

// Check for errors; throw if error is detected
// Note that due to https://github.com/ddohler/gdal-js/issues/38 this can only check for
// CEFatal errors in order to avoid raising an exception on GDALClose
if (
errorType === errorHandling.CPLErr.CEFailure ||
errorType === errorHandling.CPLErr.CEFatal
) {
let message = errorHandling.CPLGetLastErrorMsg();

throw new Error(message);
throw new Error('Error in GDALClose: ' + message);
} else {
return result;
}
Expand Down
6 changes: 3 additions & 3 deletions src/wrappers/gdalGetGeoTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export default function (GDALGetGeoTransform, errorHandling) {
Module._free(byteOffset);
let message = errorHandling.CPLGetLastErrorMsg();

throw new Error(message);
throw new Error('Error in GDALGetGeoTransform: ' + message);
} else {
// To avoid memory leaks in the Emscripten heap, we need to free up the memory we allocated
// after we've converted it into a Javascript object.
// To avoid memory leaks in the Emscripten heap, we need to free up the memory we
// allocated after we've converted it into a Javascript object.
let result = Array.from(geoTransform);

Module._free(byteOffset);
Expand Down
2 changes: 1 addition & 1 deletion src/wrappers/gdalGetProjectionRef.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function (GDALGetProjectionRef, errorHandling) {
) {
let message = errorHandling.CPLGetLastErrorMsg();

throw new Error(message);
throw new Error('Error in GDALGetProjectionRef: ' + message);
} else {
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/wrappers/gdalGetRasterCount.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function (GDALGetRasterCount, errorHandling) {
) {
let message = errorHandling.CPLGetLastErrorMsg();

throw new Error(message);
throw new Error('Error in GDALGetRasterCount: ' + message);
} else {
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/wrappers/gdalGetRasterXSize.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function (GDALGetRasterXSize, errorHandling) {
) {
let message = errorHandling.CPLGetLastErrorMsg();

throw new Error(message);
throw new Error('Error in GDALGetRasterXSize: ' + message);
} else {
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/wrappers/gdalGetRasterYSize.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function (GDALGetRasterYSize, errorHandling) {
) {
let message = errorHandling.CPLGetLastErrorMsg();

throw Error(message);
throw Error('Error in GDALGetRasterYSize: ' + message);
} else {
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/wrappers/gdalOpen.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function (GDALOpen, errorHandling, rootPath) {
FS.rmdir(directory);
let message = errorHandling.CPLGetLastErrorMsg();

throw new Error(message);
throw new Error('Error in GDALOpen: ' + message);
} else {
return {
datasetPtr: datasetPtr,
Expand Down
9 changes: 5 additions & 4 deletions src/wrappers/gdalRasterize.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ export default function (GDALRasterize, errorHandling, rootPath) {
params.deallocate();
const message = errorHandling.CPLGetLastErrorMsg();

throw new Error(message);
throw new Error('Error in GDALRasterize: ' + message);
}

// Now that we have our translate options, we need to make a file location to hold the output.
let directory = rootPath + '/' + randomKey();
// Now that we have our translate options, we need to make a file location to hold the
// output.
let directory = rootPath + randomKey();

FS.mkdir(directory);
// This makes it easier to remove later because we can just unmount rather than recursing
Expand Down Expand Up @@ -93,7 +94,7 @@ export default function (GDALRasterize, errorHandling, rootPath) {
cleanUp();
const message = errorHandling.CPLGetLastErrorMsg();

throw new Error(message);
throw new Error('Error in GDALRasterize: ' + message);
} else {
const result = {
datasetPtr: newDatasetPtr,
Expand Down
9 changes: 5 additions & 4 deletions src/wrappers/gdalTranslate.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ export default function (GDALTranslate, errorHandling, rootPath) {
params.deallocate();
const message = errorHandling.CPLGetLastErrorMsg();

throw new Error(message);
throw new Error('Error in GDALTranslate: ' + message);
}

// Now that we have our translate options, we need to make a file location to hold the output.
let directory = rootPath + '/' + randomKey();
// Now that we have our translate options, we need to make a file location to hold the
// output.
let directory = rootPath + randomKey();

FS.mkdir(directory);
// This makes it easier to remove later because we can just unmount rather than recursing
Expand Down Expand Up @@ -75,7 +76,7 @@ export default function (GDALTranslate, errorHandling, rootPath) {
cleanUp();
const message = errorHandling.CPLGetLastErrorMsg();

throw new Error(message);
throw new Error('Error in GDALTranslate: ' + message);
} else {
const result = {
datasetPtr: newDatasetPtr,
Expand Down
9 changes: 5 additions & 4 deletions src/wrappers/gdalWarp.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export default function (GDALWarp, errorHandling, rootPath) {
params.deallocate();
const message = errorHandling.CPLGetLastErrorMsg();

throw new Error(message);
throw new Error('Error in GDALWarp: ' + message);
}

let directory = rootPath + '/' + randomKey();
let directory = rootPath + randomKey();

FS.mkdir(directory);
// This makes it easier to remove later because we can just unmount rather than recursing
Expand All @@ -58,7 +58,8 @@ export default function (GDALWarp, errorHandling, rootPath) {
// at a time, we don't need to do anything fancy here.
let datasetListPtr = Module._malloc(4); // 32-bit pointer

Module.setValue(datasetListPtr, dataset, '*'); // Set datasetListPtr to the address of dataset
// Set datasetListPtr to the address of dataset
Module.setValue(datasetListPtr, dataset, '*');
let newDatasetPtr = GDALWarp(
filePath, // Output
0, // NULL because filePath is not NULL
Expand Down Expand Up @@ -87,7 +88,7 @@ export default function (GDALWarp, errorHandling, rootPath) {
cleanUp();
const message = errorHandling.CPLGetLastErrorMsg();

throw new Error(message);
throw new Error('Error in GDALWarp: ' + message);
} else {
const result = {
datasetPtr: newDatasetPtr,
Expand Down

0 comments on commit 679a56a

Please sign in to comment.