• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

thundra-io / thundra-agent-nodejs / 25be9760-4df8-4e7f-b33a-922fd84928c4

pending completion
25be9760-4df8-4e7f-b33a-922fd84928c4

push

circleci

GitHub
Add error cause as tag on user error set (#384)

290 of 493 branches covered (58.82%)

Branch coverage included in aggregate %.

3 of 3 new or added lines in 1 file covered. (100.0%)

1216 of 1822 relevant lines covered (66.74%)

24.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

52.83
/src/plugins/support/InvocationSupport.ts
1
import ThundraLogger from '../../ThundraLogger';
20✔
2
import ExecutionContextManager from '../../context/ExecutionContextManager';
20✔
3
import { ApplicationManager } from '../../application/ApplicationManager';
20✔
4

5
/**
6
 * Provides/supports API for invocation related operations
7
 */
8
class InvocationSupport {
9

10
    private constructor() {
11
    }
12

13
    /**
14
     * Sets the agent tag
15
     * @param {string} name the agent tag name
16
     * @param value the agent tag value
17
     */
18
    static setAgentTag(name: string, value: any): void {
19
        const { tags } = ExecutionContextManager.get();
92✔
20
        if (tags) {
92✔
21
            try {
92✔
22
                tags[name] = value;
92✔
23
            } catch (e) {
24
                ThundraLogger.error(`<InvocationSupport> Error occurred while setting agent tag ${name}=${value}:`, e);
×
25
            }
26
        }
27
    }
28

29
    /**
30
     * Sets the agent tags
31
     * @param tags the agent tags to be set
32
     */
33
    static setAgentTags(tagsToSet: {[name: string]: any }): void {
34
        const { tags } = ExecutionContextManager.get();
×
35
        if (tags) {
×
36
            try {
×
37
                Object.keys(tagsToSet).forEach((name) => {
×
38
                    tags[name] = tagsToSet[name];
×
39
                });
40
            } catch (e) {
41
                ThundraLogger.error(`<InvocationSupport> Error occurred while setting agent tags ${tagsToSet}:`, e);
×
42
            }
43
        }
44
    }
45

46
    /**
47
     * Gets the agent tag
48
     * @param {string} name the agent tag name
49
     * @return the tag value
50
     */
51
    static getAgentTag(name: string): any {
52
        const { tags } = ExecutionContextManager.get();
9✔
53
        if (tags) {
9!
54
            return tags[name];
9✔
55
        } else {
56
            return null;
×
57
        }
58
    }
59

60
    /**
61
     * Gets the agent tags
62
     * @return the agent tags
63
     */
64
    static getAgentTags(): any {
65
        const { tags } = ExecutionContextManager.get();
1✔
66

67
        return tags;
1✔
68
    }
69

70
    /**
71
     * Removes the agent tag
72
     * @param {string} name the agent tag name
73
     */
74
    static removeAgentTag(name: string): void {
75
        const { tags } = ExecutionContextManager.get();
×
76
        if (tags) {
×
77
            try {
×
78
                if (tags[name]) {
×
79
                    delete tags[name];
×
80
                }
81
            } catch (e) {
82
                ThundraLogger.error(`<InvocationSupport> Error occurred while removing agent tag ${name}:`, e);
×
83
            }
84
        }
85
    }
86

87
    /**
88
     * Removes the agent tags
89
     */
90
    static removeAgentTags(): void {
91
        const execContext = ExecutionContextManager.get();
1✔
92
        if (execContext) {
1✔
93
            execContext.tags = {};
1✔
94
        }
95
    }
96

97
    /**
98
     * Sets the tag
99
     * @param {string} name the tag name
100
     * @param value the tag value
101
     */
102
    static setTag(name: string, value: any): void {
103
        const { userTags } = ExecutionContextManager.get();
10✔
104
        if (userTags) {
10✔
105
            try {
10✔
106
                userTags[name] = value;
10✔
107
            } catch (e) {
108
                ThundraLogger.error(`<InvocationSupport> Error occurred while setting tag ${name}=${value}:`, e);
×
109
            }
110
        }
111
    }
112

113
    /**
114
     * Sets the tags
115
     * @param tags the tags to be set
116
     */
117
    static setTags(tagsToSet: {[name: string]: any }): void {
118
        const { userTags } = ExecutionContextManager.get();
×
119
        if (userTags) {
×
120
            try {
×
121
                Object.keys(tagsToSet).forEach((name) => {
×
122
                    userTags[name] = tagsToSet[name];
×
123
                });
124
            } catch (e) {
125
                ThundraLogger.error(`<InvocationSupport> Error occurred while setting tags ${tagsToSet}:`, e);
×
126
            }
127
        }
128
    }
129

130
    /**
131
     * Gets the tag
132
     * @param {string} name the tag name
133
     * @return the tag value
134
     */
135
    static getTag(name: string): any {
136
        const { userTags } = ExecutionContextManager.get();
8✔
137
        if (userTags) {
8!
138
            return userTags[name];
8✔
139
        } else {
140
            return null;
×
141
        }
142
    }
143

144
    /**
145
     * Gets the tags
146
     * @return the tags
147
     */
148
    static getTags() {
149
        const { userTags } = ExecutionContextManager.get();
1✔
150

151
        return userTags;
1✔
152
    }
153

154
    /**
155
     * Removes the tag
156
     * @param {string} name the tag name
157
     */
158
    static removeTag(name: string): void {
159
        const { userTags } = ExecutionContextManager.get();
×
160
        if (userTags) {
×
161
            try {
×
162
                if (userTags[name]) {
×
163
                    delete userTags[name];
×
164
                }
165
            } catch (e) {
166
                ThundraLogger.error(`<InvocationSupport> Error occurred while removing tag ${name}:`, e);
×
167
            }
168
        }
169
    }
170

171
    /**
172
     * Removes the tags
173
     */
174
    static removeTags(): void {
175
        const execContext = ExecutionContextManager.get();
1✔
176
        if (execContext) {
1✔
177
            execContext.userTags = {};
1✔
178
        }
179
    }
180

181
    /**
182
     * Checks whether invocation has error
183
     * @return {boolean} {@code true} if invocation has error, {@code false} otherwise
184
     */
185
    static hasError(): boolean {
186
        const { error, userError } = ExecutionContextManager.get();
9✔
187

188
        return (error || userError) ? true : false;
9✔
189
    }
190

191
    /**
192
     * Sets the {@link Error} to the invocation
193
     * @param {Error} error the {@link Error} to be set
194
     */
195
    static setError(error: any): void {
196
        if (error instanceof Error) {
3!
197
            const execContext = ExecutionContextManager.get();
3✔
198
            if (execContext) {
3✔
199
                execContext.userError = error;
3✔
200
                const cause: any = (error as any).cause;
3✔
201
                if (cause) {
3!
202
                    InvocationSupport.setTag('cause', {
×
203
                        name: cause.name,
204
                        message: cause.message,
205
                        stack: cause.stack,
206
                        code: cause.code,
207
                    });
208
                }
209
            }
210
        } else {
211
            ThundraLogger.debug(
×
212
                '<InvocationSupport> Skipped setting invocation error as it is not an instance of Error:', error);
213
        }
214
    }
215

216
    /**
217
     * Gets the {@link Error} to the invocation
218
     * @return {Error} the {@link Error} of the invocation
219
     */
220
    static getError(): Error {
221
        const execContext = ExecutionContextManager.get();
2✔
222
        if (execContext) {
2✔
223
            return execContext.userError;
2✔
224
        }
225
    }
226

227
    /**
228
     * Clears the invocation error
229
     */
230
    static clearError(): void {
231
        const execContext = ExecutionContextManager.get();
2✔
232
        if (execContext) {
2✔
233
            execContext.userError = null;
2✔
234
        }
235
    }
236

237
    /**
238
     * Gets the invocation URL on Thundra Console
239
     * @return {string} the invocation URL on Thundra Console
240
     */
241
    static getConsoleInvocationURL(): string {
242
        const applicationInfo = ApplicationManager.getApplicationInfo();
×
243
        const execContext = ExecutionContextManager.get();
×
244
        if (applicationInfo && applicationInfo.applicationId
×
245
            && execContext && execContext.transactionId) {
246
            return encodeURI(
×
247
                `https://apm.thundra.io/functions/${applicationInfo.applicationId}/${execContext.transactionId}/trace-chart`);
248
        }
249
        return null;
×
250
    }
251

252
}
253

254
export default InvocationSupport;
20✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc