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

feat: add debug ids to error #158

Merged
merged 2 commits into from
Feb 26, 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
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);
Loading