UNPKG

1.65 kBPlain TextView Raw
1// Copyright (c) .NET Foundation. All rights reserved.
2// Licensed under the MIT License.
3
4import { Exception, RetryContext, TraceContext } from '@azure/functions';
5import { RpcException, RpcRetryContext, RpcTraceContext } from '@azure/functions-core';
6import { copyPropIfDefined, nonNullProp } from '../utils/nonNull';
7
8export function fromRpcRetryContext(retryContext: RpcRetryContext | null | undefined): RetryContext | undefined {
9 if (!retryContext) {
10 return undefined;
11 } else {
12 const result: RetryContext = {
13 retryCount: nonNullProp(retryContext, 'retryCount'),
14 maxRetryCount: nonNullProp(retryContext, 'maxRetryCount'),
15 };
16 if (retryContext.exception) {
17 result.exception = fromRpcException(retryContext.exception);
18 }
19 return result;
20 }
21}
22
23function fromRpcException(exception: RpcException): Exception {
24 const result: Exception = {};
25 copyPropIfDefined(exception, result, 'message');
26 copyPropIfDefined(exception, result, 'source');
27 copyPropIfDefined(exception, result, 'stackTrace');
28 return result;
29}
30
31export function fromRpcTraceContext(traceContext: RpcTraceContext | null | undefined): TraceContext | undefined {
32 if (!traceContext) {
33 return undefined;
34 } else {
35 const result: TraceContext = {};
36 copyPropIfDefined(traceContext, result, 'traceParent');
37 copyPropIfDefined(traceContext, result, 'traceState');
38 if (traceContext.attributes) {
39 result.attributes = traceContext.attributes;
40 }
41 return result;
42 }
43}