apify / apify-storage-local-js

Local emulation of the apify-client NPM package, which enables local use of Apify SDK.
3 stars 4 forks source link

readAndParseFile add error handling #24

Open MSIH opened 3 years ago

MSIH commented 3 years ago

I am getting error about Json not in correct format.

Please add error handle and include path to bad file.

mnmkng commented 3 years ago

Sorry, will need a bit more info before we can look into this. It would be great if you could provide it in the following format:

Describe the bug A clear and concise description of what the bug is.

To Reproduce Add an example of code that produces the problem you're reporting. We should be able to copy your code, run it and get the same result.

Expected behavior A clear and concise description of what you expected to happen.

System information:

Additional context Add any other context about the problem here.

MSIH commented 3 years ago

I am on holiday so do not have access to the exact error. But sometimes when I run this code it will crash with an error about content not being json. this is odd because all of the files in the dataset folder were created using this library, so they should be json. The dataset has 22000 records. I am running on windows 10. This only happens when I set the offset and limit parameters. if they are set to 0 then works every time.

const openDataset = await Apify.openDataset(sourceDataset);
    const {
        items
    } = await openDataset.getData({
        offset: offset,
        limit: limit
    });

My request is you add try/catch to the _readAndParseFile function when converting to json and add to the exception message the exact file or full path to the file that failed.

Thank

private async _readAndParseFile(index: number): Promise<Record<string, unknown>> {

        const filePath = join(this.storeDir, this._getItemFileName(index));
const json = await readFile(filePath, 'utf8');
        return JSON.parse(json);
    }

looks like you already are doing it here: https://github.com/apify/apify-cli/blob/6cd38ba1d9f3574147681f255ddc37da4e0feeba/src/commands/vis.js#L9

            try {
            const inputSchemaStr = fs.readFileSync(path).toString();
            inputSchemaObj = JSON.parse(inputSchemaStr);
        } catch (err) {
            throw new Error(`Input schema is not a valid JSON (${err})`);
        }