Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js): promise handle error and rn updates #211

Merged
merged 3 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions wrappers/javascript/aries-askar-nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hyperledger/aries-askar-nodejs",
"version": "0.2.0-dev.4",
"version": "0.2.0-dev.5",
"license": "Apache-2.0",
"description": "Nodejs wrapper for Aries Askar",
"source": "src/index",
Expand Down Expand Up @@ -48,7 +48,7 @@
"dependencies": {
"@2060.io/ffi-napi": "4.0.8",
"@2060.io/ref-napi": "3.0.6",
"@hyperledger/aries-askar-shared": "0.2.0-dev.4",
"@hyperledger/aries-askar-shared": "0.2.0-dev.5",
"@mapbox/node-pre-gyp": "^1.0.10",
"node-cache": "^5.1.2",
"ref-array-di": "^1.2.2",
Expand Down
33 changes: 25 additions & 8 deletions wrappers/javascript/aries-askar-nodejs/src/NodeJSAriesAskar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ export class NodeJSAriesAskar implements AriesAskar {
deallocateCallbackBuffer(id)

if (errorCode !== 0) {
const error = this.getCurrentError()
reject(new AriesAskarError(error))
const error = this.getAriesAskarError(errorCode)
reject(error)
}

if (typeof response === 'string') {
Expand Down Expand Up @@ -196,18 +196,35 @@ export class NodeJSAriesAskar implements AriesAskar {
})
}

private handleError(errorCode: number) {
if (errorCode === 0) return

/**
* Fetch the error from the native library and throw it as a JS error
*
* NOTE:
* Checks whether the error code of the returned error matches the error code that was passed to the function.
* If it doesn't, we throw an error with the original errorCode, and a custom message explaining we weren't able
* to retrieve the error message from the native library. This should however not break functionality as long as
* error codes are used rather than error messages for error handling.
*
*/
private getAriesAskarError(errorCode: number): AriesAskarError {
const error = this.getCurrentError()
if (error.code !== errorCode) {
throw AriesAskarError.customError({
message: `Error code mismatch. Function received: '${errorCode}', but after fetch it was '${error.code}'`,
return new AriesAskarError({
code: errorCode,
message:
'Error details have already been overwritten on the native side, unable to retrieve error message for the error',
})
}

throw new AriesAskarError(error)
return new AriesAskarError(error)
}

private handleError(errorCode: number) {
if (errorCode === 0) return

throw this.getAriesAskarError(errorCode)
}

public get nativeAriesAskar() {
return getNativeAriesAskar()
}
Expand Down
4 changes: 2 additions & 2 deletions wrappers/javascript/aries-askar-react-native/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hyperledger/aries-askar-react-native",
"version": "0.2.0-dev.4",
"version": "0.2.0-dev.5",
"license": "Apache-2.0",
"description": "React Native wrapper for Aries Askar",
"main": "build/index",
Expand Down Expand Up @@ -35,7 +35,7 @@
"install": "node-pre-gyp install"
},
"dependencies": {
"@hyperledger/aries-askar-shared": "0.2.0-dev.4",
"@hyperledger/aries-askar-shared": "0.2.0-dev.5",
"@mapbox/node-pre-gyp": "^1.0.10"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,32 @@ export class ReactNativeAriesAskar implements AriesAskar {
this.ariesAskar = bindings
}

/**
* Fetch the error from the native library and return it as a JS error
*
* NOTE:
* Checks whether the error code of the returned error matches the error code that was passed to the function.
* If it doesn't, we throw an error with the original errorCode, and a custom message explaining we weren't able
* to retrieve the error message from the native library. This should however not break functionality as long as
* error codes are used rather than error messages for error handling.
*
*/
private getAriesAskarError(errorCode: number): AriesAskarError {
const error = this.getCurrentError()
if (error.code !== errorCode) {
return new AriesAskarError({
code: errorCode,
message:
'Error details have already been overwritten on the native side, unable to retrieve error message for the error',
})
}

return new AriesAskarError(error)
}

private handleError<T>({ errorCode, value }: ReturnObject<T>): T {
if (errorCode === 0) return value as T
throw new AriesAskarError(this.getCurrentError())
throw this.getAriesAskarError(errorCode)
}

private promisify(method: (cb: Callback) => void): Promise<void> {
Expand All @@ -123,8 +146,8 @@ export class ReactNativeAriesAskar implements AriesAskar {
return new Promise((resolve, reject) => {
const _cb: CallbackWithResponse<Return> = ({ errorCode, value }) => {
if (errorCode !== 0) {
const error = this.getCurrentError()
reject(new AriesAskarError(error))
const error = this.getAriesAskarError(errorCode)
reject(error)
} else {
if (value === undefined) {
reject(
Expand Down Expand Up @@ -172,7 +195,7 @@ export class ReactNativeAriesAskar implements AriesAskar {
public entryListFree(options: EntryListFreeOptions): void {
const serializedOptions = serializeArguments(options)

// null resopnse is expected as we're freeing the object
// null response is expected as we're freeing the object
this.handleError(this.ariesAskar.entryListFree(serializedOptions))
}

Expand Down
2 changes: 1 addition & 1 deletion wrappers/javascript/aries-askar-shared/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hyperledger/aries-askar-shared",
"version": "0.2.0-dev.4",
"version": "0.2.0-dev.5",
"license": "Apache-2.0",
"description": "Shared library for using Aries Askar with NodeJS and React Native",
"main": "build/index",
Expand Down
2 changes: 1 addition & 1 deletion wrappers/javascript/lerna.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "0.2.0-dev.4",
"version": "0.2.0-dev.5",
"npmClient": "yarn"
}