Skip to content

Commit

Permalink
Merge pull request #158 from yandex-cloud/feature/debug-ids-in-error
Browse files Browse the repository at this point in the history
feat: add debug ids to error
  • Loading branch information
DavyJohnes authored Feb 26, 2024
2 parents b109d61 + 3761ea8 commit 1f9a68d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Metadata } from 'nice-grpc';

export class ApiError extends Error {
metadata: Metadata;

constructor(error: Error, metadata: Metadata) {
super(error.message);
this.name = 'ApiError';
this.stack = error.stack;
this.metadata = metadata;
}

/**
* Getter for the request ID from the metadata.
* Will provide additional information in case of opening a support ticket.
* @returns {string | undefined} The request ID if it exists, undefined otherwise.
*/
get requestId(): string | undefined {
return this.metadata.get('x-request-id');
}

/**
* Getter for the server trace ID from the metadata.
* Will provide additional information in case of opening a support ticket.
* @returns {string | undefined} The server trace ID if it exists, undefined otherwise.
*/
get serverTraceId(): string | undefined {
return this.metadata.get('x-server-trace-id');
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * as cloudApi from './generated/yandex/cloud';
export * from './session';
export * from './utils/operation';
export * from './utils/decode-message';
export * as errors from './errors';
export { WrappedServiceClientType } from './types';
31 changes: 31 additions & 0 deletions src/middleware/error-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { CallOptions, ClientMiddleware, Metadata } from 'nice-grpc';
import { rethrowAbortError } from 'abort-controller-x';
import { ApiError } from '../errors';

export const errorMetadataMiddleware: ClientMiddleware = async function* errorMetadataMiddleware(
call,
options,
) {
let md: Metadata | undefined;
const { onHeader } = options;
const callOptions: CallOptions = {
...options,
onHeader: (header: Metadata) => {
md = header;
if (onHeader !== undefined) {
onHeader(header);
}
},
};

try {
return yield* call.next(call.request, callOptions);
} catch (error: unknown) {
rethrowAbortError(error);
if (error instanceof Error && md !== undefined) {
throw new ApiError(error, md);
}

throw error;
}
};
2 changes: 2 additions & 0 deletions src/utils/client-factory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { createClientFactory } from 'nice-grpc';
import { deadlineMiddleware } from 'nice-grpc-client-middleware-deadline';
import { retryMiddleware } from '../middleware/retry';
import { errorMetadataMiddleware } from '../middleware/error-metadata';

export const clientFactory = createClientFactory()
.use(errorMetadataMiddleware)
.use(retryMiddleware)
.use(deadlineMiddleware);

0 comments on commit 1f9a68d

Please sign in to comment.