Skip to content

Commit

Permalink
update error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ksyeo1010 committed Oct 6, 2023
1 parent 7cf4693 commit 6cd9110
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions binding/nodejs/src/porcupine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ export default class Porcupine {
}

const pvPorcupine = require(libraryPath); // eslint-disable-line
this._pvPorcupine = pvPorcupine;

let porcupineHandleAndStatus: PorcupineHandleAndStatus | null = null;
try {
pvPorcupine.set_sdk("node");
Expand All @@ -149,11 +151,10 @@ export default class Porcupine {

const status = porcupineHandleAndStatus!.status;
if (status !== PvStatus.SUCCESS) {
pvStatusToException(status, "Porcupine failed to initialize", pvPorcupine.get_error_stack());
this.handlePvStatus(status, "Porcupine failed to initialize");
}

this._handle = porcupineHandleAndStatus!.handle;
this._pvPorcupine = pvPorcupine;
this._frameLength = pvPorcupine.frame_length();
this._sampleRate = pvPorcupine.sample_rate();
this._version = pvPorcupine.version();
Expand Down Expand Up @@ -229,7 +230,7 @@ export default class Porcupine {

const status = keywordAndStatus!.status;
if (status !== PvStatus.SUCCESS) {
pvStatusToException(status, "Porcupine failed to process the frame", this._pvPorcupine.get_error_stack());
this.handlePvStatus(status, "Porcupine failed to process");
}
const keywordIndex = keywordAndStatus!.keyword_index;

Expand All @@ -251,4 +252,13 @@ export default class Porcupine {
console.warn("Porcupine is not initialized; nothing to destroy");
}
}

private handlePvStatus(status: PvStatus, message: string) {
const errorObject = this._pvPorcupine.get_error_stack();
if (errorObject.status === PvStatus.SUCCESS) {
pvStatusToException(status, message, errorObject.message_stack);
} else {
pvStatusToException(status, "Unable to get Porcupine error state");
}
}
}

0 comments on commit 6cd9110

Please sign in to comment.