Skip to content

Commit 2b89399

Browse files
committed
Added create and submit methods.
1 parent a950be0 commit 2b89399

File tree

1 file changed

+175
-56
lines changed

1 file changed

+175
-56
lines changed

src/exceptionless.ts

Lines changed: 175 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,83 @@ module Exceptionless {
99
this.config = new Configuration(apiKey, serverUrl);
1010
}
1111

12-
public register(handler: () => void) {}
13-
14-
//log(source:string, message:string, level?:string) {
15-
// if (!source) {
16-
// source = (<any>(arguments.callee.caller)).name;
17-
// }
18-
//
19-
// var event:IEvent = { type: 'log', source: source, message: message };
20-
// if (level) {
21-
// event.data['@level'] = level;
22-
// }
23-
//
24-
// this.submit(event);
25-
//}
26-
//
27-
//feature(feature:string) {
28-
// if (feature) {
29-
// this.submit({type: 'usage', source: feature});
30-
// }
31-
//}
32-
//
33-
//error(exception:Error) {
34-
// // TODO:
35-
//}
36-
37-
submit(event:IEvent, pluginContextData?:IContextData) {
12+
createException(exception:Error): EventBuilder {
13+
var pluginContextData = new ContextData();
14+
pluginContextData.setException(exception);
15+
return this.createEvent(pluginContextData).setType('error');
16+
}
17+
18+
submitException(exception:Error): void {
19+
this.createException(exception).submit();
20+
}
21+
22+
submitUnhandledException(exception:Error): void {
23+
var builder = this.createException(exception);
24+
builder.pluginContextData.markAsUnhandledError();
25+
builder.submit();
26+
}
27+
28+
createFeatureUsage(feature:string): EventBuilder {
29+
return this.createEvent().setType('usage').setSource(feature);
30+
}
31+
32+
submitFeatureUsage(feature:string): void {
33+
this.createFeatureUsage(feature).submit();
34+
}
35+
36+
// createLog(source:string, message:string, level?:string): EventBuilder
37+
createLog(...source_message_level:string[]): EventBuilder {
38+
var builder = this.createEvent().setType('log');
39+
40+
switch(source_message_level ? source_message_level.length : 0) {
41+
case 1:
42+
var source = (<any>(arguments.callee.caller)).name;
43+
builder = builder.setSource(source).setMessage(source_message_level[0]);
44+
break;
45+
case 2:
46+
builder = builder.setSource(source_message_level[0]).setMessage(source_message_level[1]);
47+
case 3:
48+
builder = builder.setSource(source_message_level[0]).setMessage(source_message_level[1]).setProperty('@level', source_message_level[2]);
49+
break;
50+
}
51+
52+
return builder;
53+
}
54+
55+
// submitLog(source:string, message:string, level?:string): void
56+
submitLog(...source_message_level:string[]): void {
57+
this.createLog(...source_message_level).submit();
58+
}
59+
60+
createNotFound(resource:string): EventBuilder {
61+
return this.createEvent().setType('404').setSource(resource);
62+
}
63+
64+
submitNotFound(resource:string): void {
65+
this.createNotFound(resource).submit();
66+
}
67+
68+
createSessionStart(sessionId:string): EventBuilder {
69+
return this.createEvent().setType('start').setSessionId(sessionId);
70+
}
71+
72+
submitSessionStart(sessionId:string): void {
73+
this.createSessionStart(sessionId).submit();
74+
}
75+
76+
createSessionEnd(sessionId:string): EventBuilder {
77+
return this.createEvent().setType('end').setSessionId(sessionId);
78+
}
79+
80+
submitSessionEnd(sessionId:string): void {
81+
this.createSessionEnd(sessionId).submit();
82+
}
83+
84+
createEvent(pluginContextData?:ContextData): EventBuilder {
85+
return new EventBuilder({ date: new Date() }, this, pluginContextData);
86+
}
87+
88+
submitEvent(event:IEvent, pluginContextData?:ContextData) {
3889
if (!this.config.enabled) {
3990
this.config.log.info('Event submission is currently disabled');
4091
return;
@@ -85,15 +136,21 @@ module Exceptionless {
85136

86137
export class ConsoleLog implements ILog {
87138
public info(message) {
88-
console.log('[INFO] Exceptionless:' + message)
139+
if (console && console.log) {
140+
console.log('[INFO] Exceptionless:' + message)
141+
}
89142
}
90143

91144
public warn(message) {
92-
console.log('[Warn] Exceptionless:' + message)
145+
if (console && console.log) {
146+
console.log('[Warn] Exceptionless:' + message)
147+
}
93148
}
94149

95150
public error(message) {
96-
console.log('[Error] Exceptionless:' + message)
151+
if (console && console.log) {
152+
console.log('[Error] Exceptionless:' + message)
153+
}
97154
}
98155
}
99156

@@ -494,9 +551,9 @@ module Exceptionless {
494551
export class EventBuilder {
495552
target: IEvent;
496553
client: ExceptionlessClient;
497-
pluginContextData: IContextData;
554+
pluginContextData: ContextData;
498555

499-
constructor(event:IEvent, client:ExceptionlessClient, pluginContextData?:IContextData) {
556+
constructor(event:IEvent, client:ExceptionlessClient, pluginContextData?:ContextData) {
500557
this.target = event;
501558
this.client = client;
502559
this.pluginContextData = pluginContextData;
@@ -530,24 +587,6 @@ module Exceptionless {
530587
return this;
531588
}
532589

533-
534-
private isValidIdentifier(value:string): boolean {
535-
if (value == null) {
536-
return true;
537-
}
538-
539-
if (value.length < 8 || value.length > 100) {
540-
return false;
541-
}
542-
543-
//for (int index = 0; index < value.Length; index++) {
544-
// if (!Char.IsLetterOrDigit(value[index]) && value[index] != '-')
545-
// return false;
546-
//}
547-
548-
return true;
549-
}
550-
551590
public setMessage(message:string): EventBuilder {
552591
this.target.message = message;
553592
return this;
@@ -568,35 +607,115 @@ module Exceptionless {
568607
return this;
569608
}
570609

571-
public addTags(tags:string[]): EventBuilder {
610+
public addTags(...tags:string[]): EventBuilder {
572611
if (tags == null || tags.length === 0) {
573612
return this;
574613
}
575614

576-
//this.target.tags.AddRange(tags.Where(t => !String.IsNullOrWhiteSpace(t)).Select(t => t.Trim()));
615+
if (!this.target.tags) {
616+
this.target.tags = [];
617+
}
618+
619+
for(var tag in tags) {
620+
if (tag && this.target.tags.indexOf(tag) < 0) {
621+
this.target.tags.push(tag);
622+
}
623+
}
624+
577625
return this;
578626
}
579627

580628
public setProperty(name:string, value:any): EventBuilder {
629+
if (!this.target.data) {
630+
this.target.data = {};
631+
}
632+
581633
this.target.data[name] = value;
582634
return this;
583635
}
584636

585-
public setCritical(critical:boolean): EventBuilder {
586-
// check to see if it already contains the critical tag.
637+
public markAsCritical(critical:boolean): EventBuilder {
587638
if (critical) {
588-
this.target.tags.push('Critical');
639+
this.addTags('Critical');
589640
}
590641

591642
return this;
592643
}
593644

594645
public submit(): void {
595-
this.client.submit(this.target, this.pluginContextData);
646+
this.client.submitEvent(this.target, this.pluginContextData);
647+
}
648+
649+
private isValidIdentifier(value:string): boolean {
650+
if (value == null) {
651+
return true;
652+
}
653+
654+
if (value.length < 8 || value.length > 100) {
655+
return false;
656+
}
657+
658+
for (var index = 0; index < value.length; index++) {
659+
var code = value.charCodeAt(index);
660+
var isDigit = (code >= 48) && (code <= 57);
661+
var isLetter = ((code >= 65) && (code <= 90)) || ((code >= 97) && (code <= 122));
662+
var isMinus = code === 45;
663+
664+
if (!(isDigit || isLetter) && !isMinus) {
665+
return false;
666+
}
667+
}
668+
669+
return true;
596670
}
597671
}
598672

599-
export interface IContextData {}
673+
export class ContextData {
674+
public setException(exception:Error): void {
675+
this['@@_Exception'] = exception;
676+
}
677+
678+
public hasException(): boolean {
679+
return !!this['@@_Exception']
680+
}
681+
682+
public getException(): Error {
683+
if (!this.hasException()) {
684+
return null;
685+
}
686+
687+
return this['@@_Exception'];
688+
}
689+
690+
/// <summary>
691+
/// Marks the event as being a unhandled error occurrence.
692+
/// </summary>
693+
public markAsUnhandledError(): void {
694+
this['@@_IsUnhandledError'] = true;
695+
}
696+
697+
/// <summary>
698+
/// Returns true if the event was an unhandled error.
699+
/// </summary>
700+
public isUnhandledError(): boolean {
701+
return !!this['@@_IsUnhandledError'];
702+
}
703+
704+
/// <summary>
705+
/// Sets the submission method that created the event (E.G., UnobservedTaskException)
706+
/// </summary>
707+
public setSubmissionMethod(method:string): void {
708+
this['@@_SubmissionMethod'] = method;
709+
}
710+
711+
public getSubmissionMethod(): string {
712+
if (!!this['@@_SubmissionMethod']) {
713+
return null;
714+
}
715+
716+
return this['@@_SubmissionMethod'];
717+
}
718+
}
600719

601720
export interface IUserDescription {
602721
email_address?: string;

0 commit comments

Comments
 (0)