Skip to content

Commit 78cb9f6

Browse files
committed
Added Plugin manager and the start of the error plugin.
1 parent ee54102 commit 78cb9f6

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

src/exceptionless-spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ module Exceptionless {
3030
it('should not add duplicate plugin', () => {
3131
var config = new Configuration('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw');
3232
expect(config.plugins).not.toBe(null);
33+
for (var plugin of config.plugins) {
34+
config.removePlugin(plugin);
35+
}
36+
3337
config.addPlugin('test', 20, (context:EventPluginContext) => {});
3438
config.addPlugin('test', 20, (context:EventPluginContext) => {});
3539
expect(config.plugins.length).toBe(1);
@@ -38,6 +42,10 @@ module Exceptionless {
3842
it('should generate plugin name and priority', () => {
3943
var config = new Configuration('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw');
4044
expect(config.plugins).not.toBe(null);
45+
for (var plugin of config.plugins) {
46+
config.removePlugin(plugin);
47+
}
48+
4149
config.addPlugin(null, null, (context:EventPluginContext) => {});
4250
expect(config.plugins.length).toBe(1);
4351
expect(config.plugins[0].name).not.toBe(null);
@@ -47,6 +55,10 @@ module Exceptionless {
4755
it('should sort plugins by priority', () => {
4856
var config = new Configuration('LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw');
4957
expect(config.plugins).not.toBe(null);
58+
for (var plugin of config.plugins) {
59+
config.removePlugin(plugin);
60+
}
61+
5062
config.addPlugin('3', 3, (context:EventPluginContext) => {});
5163
config.addPlugin('1', 1, (context:EventPluginContext) => {});
5264
config.addPlugin('2', 2, (context:EventPluginContext) => {});

src/exceptionless.ts

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// TODO: Process Errors
55
// TODO: Handle Server Settings
66
// TODO: Lock configuration.
7+
// TODO: Look into using templated strings `${1 + 1}`
78

89
module Exceptionless {
910
export class ExceptionlessClient {
@@ -117,6 +118,8 @@ module Exceptionless {
117118
this.apiKey = apiKey;
118119
this.serverUrl = serverUrl;
119120
this.queue = new EventQueue(this);
121+
122+
EventPluginManager.addDefaultPlugins(this);
120123
}
121124

122125
public get apiKey(): string {
@@ -178,7 +181,15 @@ module Exceptionless {
178181
}
179182
}
180183

181-
public removePlugin(name:string) {
184+
public removePlugin(plugin:IEventPlugin): void;
185+
public removePlugin(name:string): void;
186+
public removePlugin(pluginOrName:IEventPlugin|string): void {
187+
var name:string = typeof pluginOrName === 'string' ? pluginOrName : pluginOrName.name;
188+
if (!name) {
189+
this.log.error('Unable to remove plugin: No plugin name was specified.');
190+
return;
191+
}
192+
182193
for(var index = 0; index < this._plugins.length; index++) {
183194
if (this._plugins[index].name === name) {
184195
this._plugins.splice(index, 1);
@@ -369,7 +380,7 @@ module Exceptionless {
369380
private requeueEvents(events:IEvent[]) {
370381
this._config.log.info('Requeuing ' + events.length + ' events.');
371382

372-
for (var event in events) {
383+
for (var event of events) {
373384
this.enqueue(event);
374385
}
375386
}
@@ -709,7 +720,7 @@ module Exceptionless {
709720
this.target.tags = [];
710721
}
711722

712-
for(var tag in tags) {
723+
for(var tag of tags) {
713724
if (tag && this.target.tags.indexOf(tag) < 0) {
714725
this.target.tags.push(tag);
715726
}
@@ -824,6 +835,47 @@ module Exceptionless {
824835
run(context:EventPluginContext): void;
825836
}
826837

838+
class EventPluginManager {
839+
public static run(context:EventPluginContext): void {
840+
for (var plugin of context.client.config.plugins) {
841+
try {
842+
plugin.run(context);
843+
if (context.cancel) {
844+
context.log.info('Event submission cancelled by plugin "' + plugin.name + '": id=' + context.event.reference_id + ' type=' + context.event.type);
845+
return;
846+
}
847+
} catch (e) {
848+
context.log.error('An error occurred while running ' + plugin.name + '.run(): ' + e.message);
849+
}
850+
}
851+
}
852+
853+
public static addDefaultPlugins(config:Configuration): void {
854+
//config.AddPlugin<ConfigurationDefaultsPlugin>();
855+
//config.AddPlugin<EnvironmentInfoPlugin>();
856+
config.addPlugin(new ErrorPlugin());
857+
//config.AddPlugin<DuplicateCheckerPlugin>();
858+
//config.AddPlugin<SubmissionMethodPlugin>();
859+
}
860+
}
861+
862+
class ErrorPlugin implements IEventPlugin {
863+
public priority:number = 50;
864+
public name:string = 'ErrorPlugin';
865+
866+
run(context:Exceptionless.EventPluginContext): void {
867+
var exception = context.contextData.getException();
868+
if (exception == null) {
869+
return;
870+
}
871+
872+
context.event.type = 'error';
873+
874+
// TODO Parse the error.
875+
context.event.data['@error'] = exception;
876+
}
877+
}
878+
827879
export interface IUserDescription {
828880
email_address?:string;
829881
description?:string;

0 commit comments

Comments
 (0)