Skip to content

Commit

Permalink
Merge pull request #113 from velocitycareerlabs/VL-7542-fix-error-han…
Browse files Browse the repository at this point in the history
…dling

fix  error handling
  • Loading branch information
michaelavoyan authored Jul 9, 2024
2 parents 6a4b0ae + 0be7f72 commit 11174ed
Show file tree
Hide file tree
Showing 22 changed files with 62 additions and 117 deletions.
7 changes: 1 addition & 6 deletions packages/sample-server/src/controllers/CheckForOffers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
7 changes: 1 addition & 6 deletions packages/sample-server/src/controllers/FinalizeOffers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
7 changes: 1 addition & 6 deletions packages/sample-server/src/controllers/GenerateDidJwk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
7 changes: 1 addition & 6 deletions packages/sample-server/src/controllers/GenerateOffers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
7 changes: 1 addition & 6 deletions packages/sample-server/src/controllers/GenerateSignedJwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
7 changes: 1 addition & 6 deletions packages/sample-server/src/controllers/GetCountries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
7 changes: 1 addition & 6 deletions packages/sample-server/src/controllers/GetCredentialTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
7 changes: 1 addition & 6 deletions packages/sample-server/src/controllers/GetVerifiedProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
7 changes: 1 addition & 6 deletions packages/sample-server/src/controllers/SubmitPresentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
7 changes: 1 addition & 6 deletions packages/sample-server/src/controllers/VerifyJwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
4 changes: 2 additions & 2 deletions packages/sample-server/test/ImportTest.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
14 changes: 9 additions & 5 deletions packages/sample-server/test/initalization.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
20 changes: 15 additions & 5 deletions packages/sdk/src/api/entities/error/VCLError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
);
}

Expand All @@ -61,6 +62,15 @@ export default class VCLError extends Error {
};
}

private generatePayload(): Dictionary<any> {
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";
Expand Down
20 changes: 12 additions & 8 deletions packages/sdk/test/entities/VCLError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

0 comments on commit 11174ed

Please sign in to comment.