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

chore: add requestId prop to VCLError v0.9.21 #129

Merged
merged 2 commits into from
Aug 20, 2024
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
14 changes: 14 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
node_modules/**
packages/sdk/node_modules/**
packages/sample-server/node_modules/**
packages/sdk/dist/**
dist/**
babel.config.js
setupJestAfterEnv.js
.yarn/*
package.json
packages/sdk/package.json
packages/sample-server/package.json
packages/sample-app/package.json
packages/sample-app/tsconfig.json
packages/sample-app/public/*
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,4 @@ dist
.tern-port
.DS_Store
/.idea
.yarn/install-state.gz
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
2 changes: 1 addition & 1 deletion packages/sample-app/src/screens/MeinScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useState } from "react";
import React from "react";
import {
getCountries,
getCredentialTypeSchemas,
Expand Down
8 changes: 0 additions & 8 deletions packages/sample-server/.eslintignore

This file was deleted.

10 changes: 5 additions & 5 deletions packages/sample-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@
"node": ">= 20.0.0"
},
"dependencies": {
"@fastify/autoload": "~5.10.0",
"@fastify/cors": "~9.0.1",
"axios": "~1.7.2",
"env-var": "~7.5.0",
"fastify": "~4.28.1",
"fastify-plugin": "4.5.1",
"@fastify/cors": "~9.0.1",
"@fastify/autoload": "~5.10.0"
"fastify-plugin": "4.5.1"
},
"devDependencies": {
"@jest/globals": "~29.7.0",
"@types/jest": "~29.5.12",
"@types/node": "~20.14.9",
"chokidar": "~3.6.0",
"@typescript-eslint/eslint-plugin": "~5.57.0",
"@typescript-eslint/parser": "~5.57.0",
"chokidar": "~3.6.0",
"eslint": "~8.57.0",
"eslint-config-prettier": "~9.1.0",
"eslint-plugin-prettier": "~4.2.1",
"eslint-plugin-autofix": "~2.1.0",
"eslint-plugin-better-mutation": "~1.4.0",
"eslint-plugin-import": "~2.29.1",
"eslint-plugin-prefer-arrow-functions": "~3.0.1",
"eslint-plugin-prettier": "~4.2.1",
"eslint-plugin-unused-imports": "~4.0.0",
"jest": "~29.7.0",
"json-schema-to-ts": "~3.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/sample-server/test/mocks/key/KeyServiceMock.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable no-unused-vars,@typescript-eslint/no-unused-vars */
import { VCLDidJwk, VCLDidJwkDescriptor, VCLKeyService } from "@velocitycareerlabs/vnf-nodejs-wallet-sdk/src";
import { DidJwkMocks } from "../DidJwkMocks";

export class KeyServiceMock implements VCLKeyService {
// eslint-disable-next-line unused-imports/no-unused-vars,no-unused-vars
async generateDidJwk(didJwkDescriptor: VCLDidJwkDescriptor): Promise<VCLDidJwk> {
return DidJwkMocks.DidJwk
}
Expand Down
10 changes: 5 additions & 5 deletions 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.20",
"version": "0.9.21",
"description": "VNF Wallet SDK Nodejs",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -18,10 +18,10 @@
"node": ">= 18.0.0"
},
"dependencies": {
"jose": "~4.13.1",
"@velocitycareerlabs/vc-checks": "1.20.0",
"canonicalize": "^2.0.0",
"lodash": "~4.17.20",
"@velocitycareerlabs/vc-checks": "1.20.0"
"jose": "~4.13.1",
"lodash": "~4.17.20"
},
"devDependencies": {
"@jest/globals": "~29.5.0",
Expand All @@ -33,11 +33,11 @@
"@typescript-eslint/parser": "~5.57.0",
"eslint": "~8.57.0",
"eslint-config-prettier": "~9.1.0",
"eslint-plugin-prettier": "~4.2.1",
"eslint-plugin-autofix": "^1.0.0",
"eslint-plugin-better-mutation": "1.4.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-prefer-arrow-functions": "^3.0.1",
"eslint-plugin-prettier": "~4.2.1",
"eslint-plugin-unused-imports": "^4.0.0",
"jest": "~29.5.0",
"rimraf": "~4.4.1",
Expand Down
32 changes: 17 additions & 15 deletions packages/sdk/src/api/entities/error/VCLError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,59 @@ import VCLStatusCode from "./VCLStatusCode";
export default class VCLError extends Error {
payload: Nullish<string> = null;
error: Nullish<string> = null;
requestId: Nullish<string> = null;
errorCode: string = VCLErrorCode.SdkError.toString();
statusCode: Nullish<VCLStatusCode> = null;

constructor(
error: Nullish<string> = null,
errorCode: string = VCLErrorCode.SdkError.toString(),
requestId: Nullish<string> = null,
message: Nullish<string> = null,
statusCode: Nullish<VCLStatusCode> = null
) {
super(message ?? "");
this.error = error;
this.errorCode = errorCode;
this.requestId = requestId;
this.statusCode = statusCode;
this.payload = JSON.stringify(this.generatePayload());
this.payload = JSON.stringify(this.jsonObject);

this.name = "VCLError";
Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain
}

static fromPayload(payload: string): VCLError {
const payloadJson = JSON.parse(payload);
return VCLError.fromJson(payloadJson);
}

static fromJson(payloadJson: Dictionary<any>): VCLError {
const result = new VCLError(
payloadJson?.[VCLError.KeyError] || null,
payloadJson?.[VCLError.KeyErrorCode] || VCLErrorCode.SdkError.toString(),
payloadJson?.[VCLError.KeyRequestId] || null,
payloadJson?.[VCLError.KeyMessage] || null,
payloadJson?.[VCLError.KeyStatusCode] || null
);
result.payload = payload;
result.payload = JSON.stringify(payloadJson);

return result;
}

static fromError(
error: any,
error: Error | VCLError,
statusCode: number | null = null
): VCLError {
if (error instanceof VCLError) {
return error;
}
return new VCLError(
error ? (error.error ? error.error : JSON.stringify(error)) : null,
JSON.stringify(error),
VCLError.findErrorCode(error),
error ? error.message : null,
error ? (error.statusCode ?? statusCode) : statusCode,
null,
error.message,
statusCode
);
}

Expand All @@ -68,24 +77,17 @@ export default class VCLError extends Error {
return {
[VCLError.KeyPayload]: this.payload,
[VCLError.KeyError]: this.error,
[VCLError.KeyRequestId]: this.requestId,
[VCLError.KeyErrorCode]: (this.errorCode || VCLErrorCode.SdkError.toString()),
[VCLError.KeyMessage]: this.message,
[VCLError.KeyStatusCode]: this.statusCode,
};
}

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";
static readonly KeyRequestId: string = "requestId"
static readonly KeyMessage: string = "message";
static readonly KeyStatusCode: string = "statusCode";
}
1 change: 1 addition & 0 deletions packages/sdk/src/impl/utils/ProfileServiceTypeVerifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class ProfileServiceTypeVerifier {
throw new VCLError(
null,
VCLErrorCode.SdkError.toString(),
null,
this.toJsonString(
verifiedProfile.name,
`Wrong service type - expected: ${expectedServiceTypes.all}, found: ${verifiedProfile.serviceTypes.all}`
Expand Down
16 changes: 9 additions & 7 deletions packages/sdk/test/entities/VCLError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ describe("VCLError Tests", () => {
const error = new VCLError(
ErrorMocks.Error,
ErrorMocks.ErrorCode,
ErrorMocks.RequestId,
ErrorMocks.Message,
ErrorMocks.StatusCode
);

expect(error.error).toBe(ErrorMocks.Error);
expect(error.errorCode).toBe(ErrorMocks.ErrorCode);
expect(error.requestId).toBe(ErrorMocks.RequestId);
expect(error.message).toBe(ErrorMocks.Message);
expect(error.statusCode).toBe(ErrorMocks.StatusCode);
});
Expand All @@ -32,36 +34,36 @@ describe("VCLError Tests", () => {

expect(errorJsonObject[VCLError.KeyPayload]).toBe(ErrorMocks.Payload);
expect(errorJsonObject[VCLError.KeyError]).toBe(ErrorMocks.Error);
expect(errorJsonObject[VCLError.KeyErrorCode]).toBe(
ErrorMocks.ErrorCode
);
expect(errorJsonObject[VCLError.KeyErrorCode]).toBe(ErrorMocks.ErrorCode);
expect(errorJsonObject[VCLError.KeyRequestId]).toBe(ErrorMocks.RequestId);
expect(errorJsonObject[VCLError.KeyMessage]).toBe(ErrorMocks.Message);
expect(errorJsonObject[VCLError.KeyStatusCode]).toBe(
ErrorMocks.StatusCode
);
expect(errorJsonObject[VCLError.KeyStatusCode]).toBe(ErrorMocks.StatusCode);
});

test("testErrorToJsonFromProperties", () => {
const error = new VCLError(
ErrorMocks.Error,
ErrorMocks.ErrorCode,
ErrorMocks.RequestId,
ErrorMocks.Message,
ErrorMocks.StatusCode
);
const errorJsonObject = error.jsonObject;

expect(errorJsonObject[VCLError.KeyError]).toBe(ErrorMocks.Error);
expect(errorJsonObject[VCLError.KeyErrorCode]).toBe(ErrorMocks.ErrorCode);
expect(errorJsonObject[VCLError.KeyRequestId]).toBe(ErrorMocks.RequestId);
expect(errorJsonObject[VCLError.KeyMessage]).toBe(ErrorMocks.Message);
expect(errorJsonObject[VCLError.KeyStatusCode]).toBe(ErrorMocks.StatusCode);
});

test("testErrorFromSomeError", () => {
const error = VCLError.fromError(ErrorMocks.SomeErrorJson);
const error = VCLError.fromJson(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.requestId).toBe(ErrorMocks.SomeErrorJson['requestId']);
expect(error.message).toBe(ErrorMocks.SomeErrorJson['message']);
expect(error.statusCode).toBe(ErrorMocks.SomeErrorJson['statusCode']);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
class ErrorMocks {
static Payload =
'{"error":"Bad Request","errorCode": "proof_jwt_is_required","message":"proof.jwt is missing","statusCode": 400}';
'{"error":"Bad Request","errorCode":"proof_jwt_is_required","requestId":"some_request_id","message":"proof.jwt is missing","statusCode":400}';
static Error = "Bad Request";
static ErrorCode = "proof_jwt_is_required";
static RequestId = "some_request_id";
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"
"errorCode": "exchange_invalid",
"requestId": "some_request_id",
}
}

Expand Down
Loading