Skip to content

Commit

Permalink
Add optional flag to parseLogs to throw an error on decoding failure
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasgodfreyclarke committed Jul 7, 2022
1 parent 0fdfb49 commit 2660422
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions ts/src/program/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,16 @@ export class EventParser {
// its emission, thereby allowing us to know if a given log event was
// emitted by *this* program. If it was, then we parse the raw string and
// emit the event if the string matches the event being subscribed to.
public *parseLogs(logs: string[]) {
public *parseLogs(logs: string[], errorOnDecodeFailure: boolean = false) {
const logScanner = new LogScanner(logs);
const execution = new ExecutionContext();
let log = logScanner.next();
while (log !== null) {
let [event, newProgram, didPop] = this.handleLog(execution, log);
let [event, newProgram, didPop] = this.handleLog(
execution,
log,
errorOnDecodeFailure
);
if (event) {
yield event;
}
Expand All @@ -200,14 +204,15 @@ export class EventParser {
// execution stack).
private handleLog(
execution: ExecutionContext,
log: string
log: string,
errorOnDecodeFailure: boolean
): [Event | null, string | null, boolean] {
// Executing program is this program.
if (
execution.stack.length > 0 &&
execution.program() === this.programId.toString()
) {
return this.handleProgramLog(log);
return this.handleProgramLog(log, errorOnDecodeFailure);
}
// Executing program is not this program.
else {
Expand All @@ -217,14 +222,19 @@ export class EventParser {

// Handles logs from *this* program.
private handleProgramLog(
log: string
log: string,
errorOnDecodeFailure: boolean
): [Event | null, string | null, boolean] {
// This is a `msg!` log or a `sol_log_data` log.
if (log.startsWith(PROGRAM_LOG) || log.startsWith(PROGRAM_DATA)) {
const logStr = log.startsWith(PROGRAM_LOG)
? log.slice(PROGRAM_LOG_START_INDEX)
: log.slice(PROGRAM_DATA_START_INDEX);
const event = this.coder.events.decode(logStr);

if (errorOnDecodeFailure && event === null) {
throw new Error(`Unable to decode event ${logStr}`);
}
return [event, null, false];
}
// System log.
Expand Down

0 comments on commit 2660422

Please sign in to comment.