diff --git a/packages/sample-server/src/controllers/CheckForOffers.ts b/packages/sample-server/src/controllers/CheckForOffers.ts index 9a46998..40dfd9b 100644 --- a/packages/sample-server/src/controllers/CheckForOffers.ts +++ b/packages/sample-server/src/controllers/CheckForOffers.ts @@ -15,11 +15,6 @@ export async function checkForOffers(req, reply) { ); reply.send(offers); } catch (e: any) { - reply.code(500).send({ - statusCode: e.statusCode ?? "500", - error: "Failed to check offers", - message: e.stack ?? e.message ?? JSON.stringify(e), - errorCode: e.errorCode, - }); + reply.code(e.statusCode ?? 500).send(e) } } \ No newline at end of file diff --git a/packages/sample-server/src/controllers/FinalizeOffers.ts b/packages/sample-server/src/controllers/FinalizeOffers.ts index 90cfb37..b6c0c2a 100644 --- a/packages/sample-server/src/controllers/FinalizeOffers.ts +++ b/packages/sample-server/src/controllers/FinalizeOffers.ts @@ -15,11 +15,6 @@ export async function finalizeOffers(req, reply) { ); reply.send(jwtVerifiableCredentials); } catch (e: any) { - reply.code(500).send({ - statusCode: e.statusCode ?? "500", - error: "Failed to finalize offers", - message: e.stack ?? e.message ?? JSON.stringify(e), - errorCode: e.errorCode, - }); + reply.code(e.statusCode ?? 500).send(e) } } \ No newline at end of file diff --git a/packages/sample-server/src/controllers/GenerateDidJwk.ts b/packages/sample-server/src/controllers/GenerateDidJwk.ts index c0df042..7047d38 100644 --- a/packages/sample-server/src/controllers/GenerateDidJwk.ts +++ b/packages/sample-server/src/controllers/GenerateDidJwk.ts @@ -11,11 +11,6 @@ export async function generateDidJwk(req, reply) { const didJwk = await req.vclSdk.generateDidJwk(didJwkDescriptorFromJson(req.body)); reply.send(didJwk); } catch (e: any) { - reply.code(500).send({ - statusCode: e.statusCode ?? "500", - error: "Failed to generate did jwk", - message: e.stack ?? e.message ?? JSON.stringify(e), - errorCode: e.errorCode, - }); + reply.code(e.statusCode ?? 500).send(e) } } \ No newline at end of file diff --git a/packages/sample-server/src/controllers/GenerateOffers.ts b/packages/sample-server/src/controllers/GenerateOffers.ts index 7d1d358..155b11d 100644 --- a/packages/sample-server/src/controllers/GenerateOffers.ts +++ b/packages/sample-server/src/controllers/GenerateOffers.ts @@ -13,11 +13,6 @@ export async function generateOffers(req, reply) { ); reply.send(offers); } catch (e: any) { - reply.code(500).send({ - statusCode: e.statusCode ?? "500", - error: "Failed to generate offers", - message: e.stack ?? e.message ?? JSON.stringify(e), - errorCode: e.errorCode, - }); + reply.code(e.statusCode ?? 500).send(e) } } \ No newline at end of file diff --git a/packages/sample-server/src/controllers/GenerateSignedJwt.ts b/packages/sample-server/src/controllers/GenerateSignedJwt.ts index 573170b..087ca2a 100644 --- a/packages/sample-server/src/controllers/GenerateSignedJwt.ts +++ b/packages/sample-server/src/controllers/GenerateSignedJwt.ts @@ -16,11 +16,6 @@ export async function generateSignedJwt(req, reply) { ); reply.send(jwt); } catch (e: any) { - reply.code(500).send({ - statusCode: e.statusCode ?? "500", - error: "Failed to verify jwt", - message: e.stack ?? e.message ?? JSON.stringify(e), - errorCode: e.errorCode, - }); + reply.code(e.statusCode ?? 500).send(e) } } \ No newline at end of file diff --git a/packages/sample-server/src/controllers/GetCountries.ts b/packages/sample-server/src/controllers/GetCountries.ts index 92d60ed..f60ec05 100644 --- a/packages/sample-server/src/controllers/GetCountries.ts +++ b/packages/sample-server/src/controllers/GetCountries.ts @@ -9,11 +9,6 @@ export async function getCountries(req, reply) { try { reply.send(req.vclSdk.countries); } catch (e: any) { - reply.code(500).send({ - statusCode: e.statusCode ?? "500", - error: "Failed to get countries", - message: e.stack ?? e.message ?? JSON.stringify(e), - errorCode: e.errorCode, - }); + reply.code(e.statusCode ?? 500).send(e) } } \ No newline at end of file diff --git a/packages/sample-server/src/controllers/GetCredentialManifest.ts b/packages/sample-server/src/controllers/GetCredentialManifest.ts index 1069304..eaa8da7 100644 --- a/packages/sample-server/src/controllers/GetCredentialManifest.ts +++ b/packages/sample-server/src/controllers/GetCredentialManifest.ts @@ -16,11 +16,6 @@ export async function getCredentialManifest(req, reply) { ); reply.send(credentialManifest); } catch (e: any) { - reply.code(500).send({ - statusCode: e.statusCode ?? "500", - error: "Failed to search for organizations", - message: e.stack ?? e.message ?? JSON.stringify(e), - errorCode: e.errorCode, - }); + reply.code(e.statusCode ?? 500).send(e) } } \ No newline at end of file diff --git a/packages/sample-server/src/controllers/GetCredentialTypeSchemas.ts b/packages/sample-server/src/controllers/GetCredentialTypeSchemas.ts index 76a510b..ada3043 100644 --- a/packages/sample-server/src/controllers/GetCredentialTypeSchemas.ts +++ b/packages/sample-server/src/controllers/GetCredentialTypeSchemas.ts @@ -9,11 +9,6 @@ export async function getCredentialTypeSchemas(req, reply) { try { reply.send(req.vclSdk.credentialTypeSchemas); } catch (e: any) { - reply.code(500).send({ - statusCode: e.statusCode ?? "500", - error: "Failed to get credential type schemas", - message: e.stack ?? e.message ?? JSON.stringify(e), - errorCode: e.errorCode, - }); + reply.code(e.statusCode ?? 500).send(e) } } \ No newline at end of file diff --git a/packages/sample-server/src/controllers/GetCredentialTypes.ts b/packages/sample-server/src/controllers/GetCredentialTypes.ts index 375e68d..1aea9b9 100644 --- a/packages/sample-server/src/controllers/GetCredentialTypes.ts +++ b/packages/sample-server/src/controllers/GetCredentialTypes.ts @@ -9,11 +9,6 @@ export async function getCredentialTypes(req, reply) { try { reply.send(req.vclSdk.credentialTypes); } catch (e: any) { - reply.code(500).send({ - statusCode: e.statusCode ?? "500", - error: "Failed to get credential types", - message: e.stack ?? e.message ?? JSON.stringify(e), - errorCode: e.errorCode, - }); + reply.code(e.statusCode ?? 500).send(e) } } \ No newline at end of file diff --git a/packages/sample-server/src/controllers/GetCredentialTypesUIFormSchema.ts b/packages/sample-server/src/controllers/GetCredentialTypesUIFormSchema.ts index b9cb771..28c4e8d 100644 --- a/packages/sample-server/src/controllers/GetCredentialTypesUIFormSchema.ts +++ b/packages/sample-server/src/controllers/GetCredentialTypesUIFormSchema.ts @@ -14,11 +14,6 @@ export async function getCredentialTypesUIFormSchema(req, reply) { ); reply.send(credentialTypesUIFormSchema); } catch (e: any) { - reply.code(500).send({ - statusCode: e.statusCode ?? "500", - error: "Failed to get credential types UI form schema", - message: e.stack ?? e.message ?? JSON.stringify(e), - errorCode: e.errorCode, - }); + reply.code(e.statusCode ?? 500).send(e) } } \ No newline at end of file diff --git a/packages/sample-server/src/controllers/GetExchangeProgress.ts b/packages/sample-server/src/controllers/GetExchangeProgress.ts index d93ff79..0378cde 100644 --- a/packages/sample-server/src/controllers/GetExchangeProgress.ts +++ b/packages/sample-server/src/controllers/GetExchangeProgress.ts @@ -19,11 +19,6 @@ export async function getExchangeProgress (req, reply) { const exchangeProgress = await req.vclSdk.getExchangeProgress(exchangeProgressDescriptor); reply.send(exchangeProgress); } catch (e: any) { - reply.code(500).send({ - statusCode: e.statusCode ?? "500", - error: "Failed to get exchange progress", - message: e.stack ?? e.message ?? JSON.stringify(e), - errorCode: e.errorCode, - }); + reply.code(e.statusCode ?? 500).send(e) } } \ No newline at end of file diff --git a/packages/sample-server/src/controllers/GetPresentationRequest.ts b/packages/sample-server/src/controllers/GetPresentationRequest.ts index e41b887..c9976ed 100644 --- a/packages/sample-server/src/controllers/GetPresentationRequest.ts +++ b/packages/sample-server/src/controllers/GetPresentationRequest.ts @@ -14,11 +14,6 @@ export async function getPresentationRequest(req, reply) { ); reply.send(presentationRequest); } catch (e: any) { - reply.code(500).send({ - statusCode: e.statusCode ?? "500", - error: "Failed to get exchange progress", - message: e.stack ?? e.message ?? JSON.stringify(e), - errorCode: e.errorCode, - }); + reply.code(e.statusCode ?? 500).send(e) } } \ No newline at end of file diff --git a/packages/sample-server/src/controllers/GetVerifiedProfile.ts b/packages/sample-server/src/controllers/GetVerifiedProfile.ts index abb6606..31a344f 100644 --- a/packages/sample-server/src/controllers/GetVerifiedProfile.ts +++ b/packages/sample-server/src/controllers/GetVerifiedProfile.ts @@ -14,11 +14,6 @@ export async function getVerifiedProfile(req, reply) { ); reply.send(verifiedProfile); } catch (e: any) { - reply.code(500).send({ - statusCode: e.statusCode ?? "500", - error: "Failed to get credential types UI form schema", - message: e.stack ?? e.message ?? JSON.stringify(e), - errorCode: e.errorCode, - }); + reply.code(e.statusCode ?? 500).send(e) } } \ No newline at end of file diff --git a/packages/sample-server/src/controllers/SearchForOrganizations.ts b/packages/sample-server/src/controllers/SearchForOrganizations.ts index 65031c5..2b41601 100644 --- a/packages/sample-server/src/controllers/SearchForOrganizations.ts +++ b/packages/sample-server/src/controllers/SearchForOrganizations.ts @@ -14,11 +14,6 @@ export async function searchForOrganizations(req, reply) { ); reply.send(organizations); } catch (e: any) { - reply.code(500).send({ - statusCode: e.statusCode ?? "500", - error: "Failed to search for organizations", - message: e.stack ?? e.message ?? JSON.stringify(e), - errorCode: e.errorCode, - }); + reply.code(e.statusCode ?? 500).send(e) } } \ No newline at end of file diff --git a/packages/sample-server/src/controllers/SubmitPresentation.ts b/packages/sample-server/src/controllers/SubmitPresentation.ts index 20c176f..2243f12 100644 --- a/packages/sample-server/src/controllers/SubmitPresentation.ts +++ b/packages/sample-server/src/controllers/SubmitPresentation.ts @@ -12,11 +12,6 @@ export async function submitPresentation(req, reply) { const submissionResult = await req.vclSdk.submitPresentation(presentationSubmissionFromJson(req.body)); reply.send(submissionResult); } catch (e: any) { - reply.code(500).send({ - statusCode: e.statusCode ?? "500", - error: "Failed to get exchange progress", - message: e.stack ?? e.message ?? JSON.stringify(e), - errorCode: e.errorCode, - }); + reply.code(e.statusCode ?? 500).send(e) } } \ No newline at end of file diff --git a/packages/sample-server/src/controllers/VerifyJwt.ts b/packages/sample-server/src/controllers/VerifyJwt.ts index ee33708..3639e53 100644 --- a/packages/sample-server/src/controllers/VerifyJwt.ts +++ b/packages/sample-server/src/controllers/VerifyJwt.ts @@ -16,11 +16,6 @@ export async function verifyJwt(req, reply) { ); reply.send(isVerified); } catch (e: any) { - reply.code(500).send({ - statusCode: e.statusCode ?? "500", - error: "Failed to verify jwt", - message: e.stack ?? e.message ?? JSON.stringify(e), - errorCode: e.errorCode, - }); + reply.code(e.statusCode ?? 500).send(e) } } \ No newline at end of file diff --git a/packages/sample-server/test/ImportTest.test.ts b/packages/sample-server/test/ImportTest.test.ts index c7b3eea..a5bfef4 100644 --- a/packages/sample-server/test/ImportTest.test.ts +++ b/packages/sample-server/test/ImportTest.test.ts @@ -1,9 +1,9 @@ -import { VCLEnvironment } from "@velocitycareerlabs/vnf-nodejs-wallet-sdk"; +import { VCLEnvironment } from "@velocitycareerlabs/vnf-nodejs-wallet-sdk/src"; describe("Imports Test", () => { test("testEnvironment", async () => { - expect(VCLEnvironment.DEV).toBeDefined(); + expect(VCLEnvironment.Dev).toBeDefined(); }); }); diff --git a/packages/sample-server/test/initalization.test.ts b/packages/sample-server/test/initalization.test.ts index 9efee4c..56bf725 100644 --- a/packages/sample-server/test/initalization.test.ts +++ b/packages/sample-server/test/initalization.test.ts @@ -1,13 +1,15 @@ -"use strict"; -import { describe, expect, test, beforeAll, afterAll } from "@jest/globals"; +import { afterAll, beforeAll, describe, expect, test } from "@jest/globals"; import app from "../src/App"; import { FastifyInstance } from "fastify"; import { VCLImpl } from "@velocitycareerlabs/vnf-nodejs-wallet-sdk/src/impl/VCLImpl"; -import VCLInitializationDescriptor from "packages/sdk/src/api/entities/initialization/VCLInitializationDescriptor"; +import { + VCLCryptoServicesDescriptor, + VCLInitializationDescriptor, + VCLXVnfProtocolVersion +} from "@velocitycareerlabs/vnf-nodejs-wallet-sdk/src"; import VCLCountries from "@velocitycareerlabs/vnf-nodejs-wallet-sdk/src/api/entities/VCLCountries"; import VCLEnvironment from "@velocitycareerlabs/vnf-nodejs-wallet-sdk/src/api/VCLEnvironment"; -import VCLCryptoServicesDescriptor from "packages/sdk/src/api/entities/initialization/VCLCryptoServicesDescriptor"; import { JwtSignServiceMock } from "./mocks/jwt/JwtSignServiceMock"; import { JwtVerifyServiceMock } from "./mocks/jwt/JwtVerifyServiceMock"; import { KeyServiceMock } from "./mocks/key/KeyServiceMock"; @@ -25,7 +27,9 @@ describe("initalization flow", () => { test("App initialization", async () => { await vcl.initialize( - new VCLInitializationDescriptor(VCLEnvironment.DEV, + new VCLInitializationDescriptor( + VCLEnvironment.Dev, + VCLXVnfProtocolVersion.XVnfProtocolVersion2, new VCLCryptoServicesDescriptor( new KeyServiceMock(), new JwtSignServiceMock(), diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 94b6ea4..7689160 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -1,6 +1,6 @@ { "name": "@velocitycareerlabs/vnf-nodejs-wallet-sdk", - "version": "0.9.14", + "version": "0.9.15", "description": "VNF Wallet SDK Nodejs", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/packages/sdk/src/api/entities/error/VCLError.ts b/packages/sdk/src/api/entities/error/VCLError.ts index 784014b..847c3e0 100644 --- a/packages/sdk/src/api/entities/error/VCLError.ts +++ b/packages/sdk/src/api/entities/error/VCLError.ts @@ -18,6 +18,7 @@ export default class VCLError extends Error { this.error = error; this.errorCode = errorCode; this.statusCode = statusCode; + this.payload = JSON.stringify(this.generatePayload()); this.name = "VCLError"; Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain @@ -37,17 +38,17 @@ export default class VCLError extends Error { } static fromError( - error: Error, + error: any, statusCode: number | null = null ): VCLError { if (error instanceof VCLError) { return error; } return new VCLError( - null, - VCLErrorCode.SdkError.toString(), - error.message ?? JSON.stringify(error), - statusCode + error.error, + error.errorCode, + error.message, + error.statusCode ?? statusCode, ); } @@ -61,6 +62,15 @@ export default class VCLError extends Error { }; } + private generatePayload(): Dictionary { + return { + [VCLError.KeyError]: this.error, + [VCLError.KeyErrorCode]: this.errorCode, + [VCLError.KeyMessage]: this.message, + [VCLError.KeyStatusCode]: this.statusCode, + }; + } + static readonly KeyPayload: string = "payload"; static readonly KeyError: string = "error"; static readonly KeyErrorCode: string = "errorCode"; diff --git a/packages/sdk/test/entities/VCLError.test.ts b/packages/sdk/test/entities/VCLError.test.ts index 8bc1814..0dc67dc 100644 --- a/packages/sdk/test/entities/VCLError.test.ts +++ b/packages/sdk/test/entities/VCLError.test.ts @@ -20,7 +20,6 @@ describe("VCLError Tests", () => { ErrorMocks.StatusCode ); - expect(error.payload).toBeNull(); expect(error.error).toBe(ErrorMocks.Error); expect(error.errorCode).toBe(ErrorMocks.ErrorCode); expect(error.message).toBe(ErrorMocks.Message); @@ -51,14 +50,19 @@ describe("VCLError Tests", () => { ); const errorJsonObject = error.jsonObject; - expect(errorJsonObject[VCLError.KeyPayload]).toBeFalsy(); expect(errorJsonObject[VCLError.KeyError]).toBe(ErrorMocks.Error); - expect(errorJsonObject[VCLError.KeyErrorCode]).toBe( - ErrorMocks.ErrorCode - ); + expect(errorJsonObject[VCLError.KeyErrorCode]).toBe(ErrorMocks.ErrorCode); expect(errorJsonObject[VCLError.KeyMessage]).toBe(ErrorMocks.Message); - expect(errorJsonObject[VCLError.KeyStatusCode]).toBe( - ErrorMocks.StatusCode - ); + expect(errorJsonObject[VCLError.KeyStatusCode]).toBe(ErrorMocks.StatusCode); + }); + + test("testErrorFromSomeError", () => { + const error = VCLError.fromError(ErrorMocks.SomeErrorJson); + + expect(JSON.parse(error.payload ?? "{}")).toStrictEqual(ErrorMocks.SomeErrorJson); + expect(error.error).toBe(ErrorMocks.SomeErrorJson['error']); + expect(error.errorCode).toBe(ErrorMocks.SomeErrorJson['errorCode']); + expect(error.message).toBe(ErrorMocks.SomeErrorJson['message']); + expect(error.statusCode).toBe(ErrorMocks.SomeErrorJson['statusCode']); }); }); diff --git a/packages/sdk/test/infrastructure/resources/valid/ErrorMocks.ts b/packages/sdk/test/infrastructure/resources/valid/ErrorMocks.ts index 611f839..de233c8 100644 --- a/packages/sdk/test/infrastructure/resources/valid/ErrorMocks.ts +++ b/packages/sdk/test/infrastructure/resources/valid/ErrorMocks.ts @@ -5,6 +5,13 @@ class ErrorMocks { static ErrorCode = "proof_jwt_is_required"; static Message = "proof.jwt is missing"; static StatusCode = 400; + + static SomeErrorJson = { + "error": "Bad Request", + "message": "Exchange 668306be0a90a4bff3c4707a is in an invalid state", + "statusCode": 400, + "errorCode": "exchange_invalid" + } } export { ErrorMocks };