-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from yandex-cloud/feature/debug-ids-in-error
feat: add debug ids to error
- Loading branch information
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |