Add optional flag to parseLogs to throw an error on decoding failure (#2043)
* Add optional flag to parseLogs to throw an error on decoding failure * update changelog Co-authored-by: henrye <henry@notanemail>
This commit is contained in:
parent
fb714b9343
commit
66e45327b9
|
@ -32,6 +32,7 @@ The minor version will be incremented upon a breaking change and the patch versi
|
||||||
- lang: Updates `AccountsClose` to make it safe to call manually ([#2209](https://github.com/coral-xyz/anchor/pull/2209))
|
- lang: Updates `AccountsClose` to make it safe to call manually ([#2209](https://github.com/coral-xyz/anchor/pull/2209))
|
||||||
- lang: Update rust used in the repo version 1.62 ([#2272](https://github.com/coral-xyz/anchor/pull/2272))
|
- lang: Update rust used in the repo version 1.62 ([#2272](https://github.com/coral-xyz/anchor/pull/2272))
|
||||||
- cli: Allow custom cluster config ([#2271](https://github.com/coral-xyz/anchor/pull/2271)).
|
- cli: Allow custom cluster config ([#2271](https://github.com/coral-xyz/anchor/pull/2271)).
|
||||||
|
- ts: Add optional flag to parseLogs to throw an error on decoding failure ([#2043](https://github.com/coral-xyz/anchor/pull/2043))
|
||||||
|
|
||||||
### Fixes
|
### Fixes
|
||||||
|
|
||||||
|
|
|
@ -176,12 +176,16 @@ export class EventParser {
|
||||||
// its emission, thereby allowing us to know if a given log event was
|
// 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
|
// 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.
|
// 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 logScanner = new LogScanner(logs);
|
||||||
const execution = new ExecutionContext();
|
const execution = new ExecutionContext();
|
||||||
let log = logScanner.next();
|
let log = logScanner.next();
|
||||||
while (log !== null) {
|
while (log !== null) {
|
||||||
let [event, newProgram, didPop] = this.handleLog(execution, log);
|
let [event, newProgram, didPop] = this.handleLog(
|
||||||
|
execution,
|
||||||
|
log,
|
||||||
|
errorOnDecodeFailure
|
||||||
|
);
|
||||||
if (event) {
|
if (event) {
|
||||||
yield event;
|
yield event;
|
||||||
}
|
}
|
||||||
|
@ -201,14 +205,15 @@ export class EventParser {
|
||||||
// execution stack).
|
// execution stack).
|
||||||
private handleLog(
|
private handleLog(
|
||||||
execution: ExecutionContext,
|
execution: ExecutionContext,
|
||||||
log: string
|
log: string,
|
||||||
|
errorOnDecodeFailure: boolean
|
||||||
): [Event | null, string | null, boolean] {
|
): [Event | null, string | null, boolean] {
|
||||||
// Executing program is this program.
|
// Executing program is this program.
|
||||||
if (
|
if (
|
||||||
execution.stack.length > 0 &&
|
execution.stack.length > 0 &&
|
||||||
execution.program() === this.programId.toString()
|
execution.program() === this.programId.toString()
|
||||||
) {
|
) {
|
||||||
return this.handleProgramLog(log);
|
return this.handleProgramLog(log, errorOnDecodeFailure);
|
||||||
}
|
}
|
||||||
// Executing program is not this program.
|
// Executing program is not this program.
|
||||||
else {
|
else {
|
||||||
|
@ -218,7 +223,8 @@ export class EventParser {
|
||||||
|
|
||||||
// Handles logs from *this* program.
|
// Handles logs from *this* program.
|
||||||
private handleProgramLog(
|
private handleProgramLog(
|
||||||
log: string
|
log: string,
|
||||||
|
errorOnDecodeFailure: boolean
|
||||||
): [Event | null, string | null, boolean] {
|
): [Event | null, string | null, boolean] {
|
||||||
// This is a `msg!` log or a `sol_log_data` log.
|
// This is a `msg!` log or a `sol_log_data` log.
|
||||||
if (log.startsWith(PROGRAM_LOG) || log.startsWith(PROGRAM_DATA)) {
|
if (log.startsWith(PROGRAM_LOG) || log.startsWith(PROGRAM_DATA)) {
|
||||||
|
@ -226,6 +232,10 @@ export class EventParser {
|
||||||
? log.slice(PROGRAM_LOG_START_INDEX)
|
? log.slice(PROGRAM_LOG_START_INDEX)
|
||||||
: log.slice(PROGRAM_DATA_START_INDEX);
|
: log.slice(PROGRAM_DATA_START_INDEX);
|
||||||
const event = this.coder.events.decode(logStr);
|
const event = this.coder.events.decode(logStr);
|
||||||
|
|
||||||
|
if (errorOnDecodeFailure && event === null) {
|
||||||
|
throw new Error(`Unable to decode event ${logStr}`);
|
||||||
|
}
|
||||||
return [event, null, false];
|
return [event, null, false];
|
||||||
}
|
}
|
||||||
// System log.
|
// System log.
|
||||||
|
|
Loading…
Reference in New Issue