FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Propagate JSON parse errors by StantonMatt · Pull Request #326 · extrabacon/python-shell · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .ts  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
55 changes: 54 additions & 1 deletion index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ export class PythonShellErrorWithLogs extends PythonShellError {
logs: any[];
}

export class PythonShellParseError extends PythonShellError {
data: string;
originalError: Error;

constructor(data: string, originalError: Error) {
super('Error parsing PythonShell output: ' + originalError.message);
this.name = 'PythonShellParseError';
this.data = data;
this.originalError = originalError;
}
}

/**
* Takes in a string stream and emits batches seperated by newlines
*/
Expand Down Expand Up @@ -169,6 +181,7 @@ export class PythonShell extends EventEmitter {

let self = this;
let errorData = '';
let parseError: PythonShellParseError;
EventEmitter.call(this);

options = <Options>extend({}, PythonShell.defaultOptions, options);
Expand Down Expand Up @@ -205,7 +218,14 @@ export class PythonShell extends EventEmitter {
// note that setting the encoding turns the chunk into a string
stdoutSplitter.setEncoding(options.encoding || 'utf8');
this.stdout.pipe(stdoutSplitter).on('data', (chunk: string) => {
this.emit('message', self.parser(chunk));
let parsedChunk;
try {
parsedChunk = self.parser(chunk);
} catch (err) {
emitParseError(err, chunk);
return;
}
this.emit('message', parsedChunk);
});
}

Expand Down Expand Up @@ -249,6 +269,15 @@ export class PythonShell extends EventEmitter {
terminateIfNeeded();
});

function emitParseError(err: unknown, data: string) {
let originalError = err instanceof Error ? err : new Error('' + err);
let outputError = new PythonShellParseError(data, originalError);
if (!parseError) {
parseError = outputError;
}
self.emit('parseError', outputError);
}

function terminateIfNeeded() {
if (
!self.stderrHasEnded ||
Expand Down Expand Up @@ -277,6 +306,8 @@ export class PythonShell extends EventEmitter {
if (self.listeners('pythonError').length || !self._endCallback) {
self.emit('pythonError', err);
}
} else if (parseError) {
err = parseError;
}

self.terminated = true;
Expand Down Expand Up @@ -532,4 +563,26 @@ export interface PythonShell {
event: 'pythonError',
listener: (error: PythonShellError) => void,
): this;

addListener(
event: 'parseError',
listener: (error: PythonShellParseError) => void,
): this;
emit(event: 'parseError', error: PythonShellParseError): boolean;
on(
event: 'parseError',
listener: (error: PythonShellParseError) => void,
): this;
once(
event: 'parseError',
listener: (error: PythonShellParseError) => void,
): this;
prependListener(
event: 'parseError',
listener: (error: PythonShellParseError) => void,
): this;
prependOnceListener(
event: 'parseError',
listener: (error: PythonShellParseError) => void,
): this;
}
1 change: 1 addition & 0 deletions test/python/invalid_json.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("not json")
24 changes: 24 additions & 0 deletions test/test-python-shell.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,30 @@ describe('PythonShell', function () {
})
.end(done);
});
it('should emit parseError when JSON output cannot be parsed', function (done) {
let pyshell = new PythonShell('invalid_json.py', {
mode: 'json',
});
pyshell
.on('parseError', function (err) {
err.name.should.be.exactly('PythonShellParseError');
err.data.should.be.exactly('not json');
err.originalError.should.be.instanceOf(SyntaxError);
})
.end(function (err) {
err.name.should.be.exactly('PythonShellParseError');
done();
});
});
it('should reject run() when JSON output cannot be parsed', async function () {
try {
await PythonShell.run('invalid_json.py', { mode: 'json' });
throw new Error('Expected run() to reject invalid JSON output');
} catch (err) {
err.name.should.be.exactly('PythonShellParseError');
err.data.should.be.exactly('not json');
}
});
it('should properly buffer partial messages', function (done) {
// echo_text_with_newline_control echoes text with $'s replaced with newlines
let pyshell = new PythonShell('echo_text_with_newline_control.py', {
Expand Down

Back | FazBrowse Home | New Git URL