Skip to content

Commit 2efb497

Browse files
dawnmistkamilogorek
authored andcommitted
feat: Typescript typings update (#1183)
* Updates to typescript types definitions. * Update captureException to match support of ErrorEvent and string as permitted inputs for the exception (fixes issue #1167). * Update setExtraContext to allow using setExtraContext() to clear the extra data. * Update setTagsContext to allow using setTagsContext() to clear the tags data. * Add tests for the above changes. * Add additional test calls for the Raven.context and Raven.wrap to take a function that throws an ErrorEvent. * Update minimum Typescript version to 2.3.0, as the type definitions for ErrorEvent are incorrect/unusable in versions of Typescript less than 2.3.0. * Update usage documentation - add documentation for how to remove a tag from the tag context data, and how to remove a value from the extra context data, as described in issue #614. * Add missing console log levels to LogLevel.
1 parent edbc49d commit 2efb497

File tree

4 files changed

+73
-16
lines changed

4 files changed

+73
-16
lines changed

docs/usage.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,19 @@ functions all allow passing additional data to be tagged onto the error.
142142
143143
Raven.setTagsContext({ key: "value" });
144144
145+
Tags given in ``setTagsContext`` are merged with the existing tags. If you need to remove a tag,
146+
use ``getContext`` to get the current context value, call ``setTagsContext`` with no parameters
147+
to remove all tags context data, and then call ``setTagsContext`` again with the tags that you
148+
want to keep.
149+
150+
.. code-block:: javascript
151+
152+
const context = Raven.getContext(); // Note: Do not mutate context directly.
153+
const tags = {...context.tags};
154+
delete tags['TagNameToDelete'];
155+
Raven.setTagsContext(); // Clear all current tags from the context.
156+
Raven.setTagsContext(tags); // Add back the tags you want to keep.
157+
145158
.. describe:: extra
146159

147160
Arbitrary data to associate with the event.
@@ -160,6 +173,18 @@ functions all allow passing additional data to be tagged onto the error.
160173
161174
Raven.setExtraContext({ foo: "bar" })
162175
176+
Data given in ``setExtraContext`` is merged with the existing extra data. If you need to remove
177+
a field from the extra context data, use ``getContext`` to get the current context value, call
178+
``setExtraContext`` with no parameters to remove all extra context data, and then call
179+
``setExtraContext`` again with the extra data content that you want to keep.
180+
181+
.. code-block:: javascript
182+
183+
const context = Raven.getContext(); // Note: Do not mutate context directly.
184+
const extra = {...context.extra};
185+
delete extra['FieldKeyToDelete'];
186+
Raven.setExtraContext(); // Clear all extra data from the context.
187+
Raven.setExtraContext(extra); // Add back the extra data that you want to keep.
163188
164189
.. _raven-js-recording-breadcrumbs:
165190

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"proxyquireify": "^3.0.2",
6868
"sinon": "^3.2.1",
6969
"through2": "^2.0.0",
70-
"typescript": "^1.8.10",
70+
"typescript": "^2.3.0",
7171
"whatwg-fetch": "^2.0.3"
7272
},
7373
"prettier": {

typescript/raven-tests.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,42 @@ try {
4545
Raven.captureException(e, {tags: { key: "value" }});
4646
}
4747

48+
// ErrorEvent requires at least Typescript 2.3.0 due to incorrect ErrorEvent definitions
49+
// prior to that version.
50+
var throwsErrorEvent = () => {
51+
throw new ErrorEvent('oops', {error: new Error('Oops')});
52+
};
53+
54+
try {
55+
throwsErrorEvent();
56+
} catch(ee) {
57+
Raven.captureException(ee);
58+
Raven.captureException(ee, {tags: { key: "value" }});
59+
}
60+
61+
Raven.captureException('Something broke');
62+
Raven.captureException('Something broke', {tags: { key: "value" }});
63+
4864
Raven.context(throwsError);
4965
Raven.context({tags: { key: "value" }}, throwsError);
66+
Raven.context(throwsErrorEvent);
67+
Raven.context({tags: { key: "value" }}, throwsErrorEvent);
5068

5169
setTimeout(Raven.wrap(throwsError), 1000);
5270
Raven.wrap({logger: "my.module"}, throwsError)();
71+
Raven.wrap(throwsErrorEvent)();
72+
Raven.wrap({logger: "my.module"}, throwsErrorEvent)();
5373

74+
Raven.setUserContext();
5475
Raven.setUserContext({
5576
5677
id: '123'
5778
});
5879

5980
Raven.setExtraContext({foo: 'bar'});
81+
Raven.setExtraContext();
6082
Raven.setTagsContext({env: 'prod'});
83+
Raven.setTagsContext();
6184
Raven.clearContext();
6285
var obj:Object = Raven.getContext();
6386
var err:Error = Raven.lastException();
@@ -69,6 +92,7 @@ Raven.captureMessage('Warning', { level: 'warning' });
6992
Raven.captureBreadcrumb({
7093
message: "This is a breadcrumb message."
7194
});
95+
Raven.captureBreadcrumb({category: "console", level: "log", message: "A console.log() message"});
7296

7397
Raven.setRelease('abc123');
7498
Raven.setEnvironment('production');

typescript/raven.d.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ declare module Raven {
6565

6666
/** Append headers to the fetch or XMLHttpRequest request. Should be in a form of hash, were value can be string or function */
6767
headers?: {
68-
[key: string]: (string | Function);
68+
[key: string]: (string | Function);
6969
};
7070

7171
/** `fetch` init parameters */
7272
fetchParameters?: {
73-
[key: string]: (string | Function);
73+
[key: string]: (string | Function);
7474
};
7575

7676
/** Allow use of private/secretKey. */
@@ -187,11 +187,11 @@ declare module Raven {
187187
/*
188188
* Manually capture an exception and send it over to Sentry
189189
*
190-
* @param {error} ex An exception to be logged
190+
* @param {error|ErrorEvent|string} ex An exception to be logged
191191
* @param {object} options A specific set of options for this error [optional]
192192
* @return {Raven}
193193
*/
194-
captureException(ex: Error, options?: RavenOptions): RavenStatic;
194+
captureException(ex: Error | ErrorEvent | string, options?: RavenOptions): RavenStatic;
195195

196196
/*
197197
* Manually send a message to Sentry
@@ -205,28 +205,36 @@ declare module Raven {
205205
/** Log a breadcrumb */
206206
captureBreadcrumb(crumb: Breadcrumb): RavenStatic;
207207

208-
/**
209-
* Clear the user context, removing the user data that would be sent to Sentry.
210-
*/
211-
setUserContext(): RavenStatic;
212-
213208
/*
214209
* Set a user to be sent along with the payload.
215210
*
216211
* @param {object} user An object representing user data [optional]
212+
* If user is undefined, the current user context will be removed.
217213
* @return {Raven}
218214
*/
219-
setUserContext(user: {
215+
setUserContext(user?: {
220216
id?: string;
221217
username?: string;
222218
email?: string;
223219
}): RavenStatic;
224220

225-
/** Merge extra attributes to be sent along with the payload. */
226-
setExtraContext(context: Object): RavenStatic;
221+
/*
222+
* Merge extra attributes to be sent along with the payload.
223+
*
224+
* @param {object} context A set of data to be merged with the current extra context data [optional]
225+
* If context is undefined, the current extra context data will be removed.
226+
* @return {Raven}
227+
*/
228+
setExtraContext(context?: Object): RavenStatic;
227229

228-
/** Merge tags to be sent along with the payload. */
229-
setTagsContext(tags: Object): RavenStatic;
230+
/*
231+
* Merge tags to be sent along with the payload.
232+
*
233+
* @param {object} tags A set of data to be merged with the current tag context data [optional]
234+
* If tags is undefined, the current tag context data will be removed.
235+
* @return {Raven}
236+
*/
237+
setTagsContext(tags?: Object): RavenStatic;
230238

231239
/** Clear all of the context. */
232240
clearContext(): RavenStatic;
@@ -308,5 +316,5 @@ declare module Raven {
308316
sentry?: boolean;
309317
}
310318

311-
type LogLevel = "critical" | "error" | "warning" | "info" | "debug";
319+
type LogLevel = "critical" | "error" | "warning" | "info" | "debug" | "warn" | "log";
312320
}

0 commit comments

Comments
 (0)