Skip to content

Change models to classes and re-arrange folder structure #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
"eslint.validate": [
"javascript",
"typescript"
]
],
"deno.enable": false
}
2 changes: 2 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
{
"type": "npm",
"script": "clean",
"problemMatcher": [],
"label": "npm: clean"
},
{
"type": "npm",
Expand Down
16 changes: 8 additions & 8 deletions packages/browser/src/services/DefaultErrorParser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
EventPluginContext,
IError,
ErrorInfo,
IErrorParser,
IParameter,
IStackFrame
ParameterInfo,
StackFrameInfo
} from '@exceptionless/core';

import {
Expand All @@ -12,21 +12,21 @@ import {
} from 'stacktrace-js';

export class DefaultErrorParser implements IErrorParser {
public async parse(context: EventPluginContext, exception: Error): Promise<IError> {
function getParameters(parameters: string | string[]): IParameter[] {
public async parse(context: EventPluginContext, exception: Error): Promise<ErrorInfo> {
function getParameters(parameters: string | string[]): ParameterInfo[] {
const params: string[] = (typeof parameters === 'string' ? [parameters] : parameters) || [];

const items: IParameter[] = [];
const items: ParameterInfo[] = [];
for (const param of params) {
items.push({ name: param });
}

return items;
}

function getStackFrames(stackFrames: StackFrame[]): IStackFrame[] {
function getStackFrames(stackFrames: StackFrame[]): StackFrameInfo[] {
const ANONYMOUS: string = '<anonymous>';
const frames: IStackFrame[] = [];
const frames: StackFrameInfo[] = [];

for (const frame of stackFrames) {
const fileName: string = frame.getFileName();
Expand Down
23 changes: 12 additions & 11 deletions packages/browser/src/services/DefaultModuleCollector.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import {
IModule,
IModuleCollector,
getHashCode,
parseVersion
} from '@exceptionless/core';
IModuleCollector,
ModuleInfo,
parseVersion,
} from "@exceptionless/core";

export class DefaultModuleCollector implements IModuleCollector {
public getModules(): IModule[] {
public getModules(): ModuleInfo[] {
if (!document || !document.getElementsByTagName) {
return null;
}

const modules: IModule[] = [];
const scripts: HTMLCollectionOf<HTMLScriptElement> = document.getElementsByTagName('script');
const modules: ModuleInfo[] = [];
const scripts: HTMLCollectionOf<HTMLScriptElement> = document
.getElementsByTagName("script");
if (scripts && scripts.length > 0) {
for (let index = 0; index < scripts.length; index++) {
if (scripts[index].src) {
modules.push({
module_id: index,
name: scripts[index].src.split('?')[0],
version: parseVersion(scripts[index].src)
name: scripts[index].src.split("?")[0],
version: parseVersion(scripts[index].src),
});
} else if (scripts[index].innerHTML) {
modules.push({
module_id: index,
name: 'Script Tag',
version: getHashCode(scripts[index].innerHTML).toString()
name: "Script Tag",
version: getHashCode(scripts[index].innerHTML).toString(),
});
}
}
Expand Down
27 changes: 16 additions & 11 deletions packages/browser/src/services/DefaultRequestInfoCollector.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import {
EventPluginContext,
IRequestInfo,
IRequestInfoCollector,
getCookies,
parseQueryString
} from '@exceptionless/core';
IRequestInfoCollector,
parseQueryString,
RequestInfo,
} from "@exceptionless/core";

export class DefaultRequestInfoCollector implements IRequestInfoCollector {
public getRequestInfo(context: EventPluginContext): IRequestInfo {
public getRequestInfo(context: EventPluginContext): RequestInfo {
if (!document || !navigator || !location) {
return null;
}

const config = context.client.config;
const exclusions = config.dataExclusions;
const requestInfo: IRequestInfo = {
const requestInfo: RequestInfo = {
user_agent: navigator.userAgent,
is_secure: location.protocol === 'https:',
is_secure: location.protocol === "https:",
host: location.hostname,
port: location.port && location.port !== '' ? parseInt(location.port, 10) : 80,
path: location.pathname
port: location.port && location.port !== ""
? parseInt(location.port, 10)
: 80,
path: location.pathname,
// client_ip_address: 'TODO'
};

Expand All @@ -28,10 +30,13 @@ export class DefaultRequestInfoCollector implements IRequestInfoCollector {
}

if (config.includeQueryString) {
requestInfo.query_string = parseQueryString(location.search.substring(1), exclusions);
requestInfo.query_string = parseQueryString(
location.search.substring(1),
exclusions,
);
}

if (document.referrer && document.referrer !== '') {
if (document.referrer && document.referrer !== "") {
requestInfo.referrer = document.referrer;
}

Expand Down
98 changes: 67 additions & 31 deletions packages/core/src/EventBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { ExceptionlessClient } from './ExceptionlessClient.js';
import { IEvent } from './models/IEvent.js';
import { IManualStackingInfo } from './models/IManualStackingInfo.js';
import { IRequestInfo } from "./models/IRequestInfo.js";
import { IUserInfo } from './models/IUserInfo.js';
import { ContextData } from './plugins/ContextData.js';
import { EventPluginContext } from './plugins/EventPluginContext.js';
import { addRange, stringify, isEmpty } from "./Utils.js";
import { ExceptionlessClient } from "./ExceptionlessClient.js";
import { Event } from "./models/Event.js";
import { ManualStackingInfo } from "./models/data/ManualStackingInfo.js";
import { RequestInfo } from "./models/data/RequestInfo.js";
import { UserInfo } from "./models/data/UserInfo.js";
import { ContextData } from "./plugins/ContextData.js";
import { EventPluginContext } from "./plugins/EventPluginContext.js";
import { addRange, isEmpty, stringify } from "./Utils.js";

export class EventBuilder {
public target: IEvent;
public target: Event;
public client: ExceptionlessClient;
public pluginContextData: ContextData;

private _validIdentifierErrorMessage: string = 'must contain between 8 and 100 alphanumeric or \'-\' characters.'; // optimization for minifier.
private _validIdentifierErrorMessage: string =
"must contain between 8 and 100 alphanumeric or '-' characters."; // optimization for minifier.

constructor(event: IEvent, client: ExceptionlessClient, pluginContextData?: ContextData) {
constructor(
event: Event,
client: ExceptionlessClient,
pluginContextData?: ContextData,
) {
this.target = event;
this.client = client;
this.pluginContextData = pluginContextData || new ContextData();
Expand Down Expand Up @@ -53,14 +58,14 @@ export class EventBuilder {
*/
public setEventReference(name: string, id: string): EventBuilder {
if (!name) {
throw new Error('Invalid name');
throw new Error("Invalid name");
}

if (!id || !this.isValidIdentifier(id)) {
throw new Error(`Id ${this._validIdentifierErrorMessage}`);
}

this.setProperty('@ref:' + name, id);
this.setProperty("@ref:" + name, id);
return this;
}

Expand All @@ -74,27 +79,34 @@ export class EventBuilder {

public setGeo(latitude: number, longitude: number): EventBuilder {
if (latitude < -90.0 || latitude > 90.0) {
throw new Error('Must be a valid latitude value between -90.0 and 90.0.');
throw new Error("Must be a valid latitude value between -90.0 and 90.0.");
}

if (longitude < -180.0 || longitude > 180.0) {
throw new Error('Must be a valid longitude value between -180.0 and 180.0.');
throw new Error(
"Must be a valid longitude value between -180.0 and 180.0.",
);
}

this.target.geo = `${latitude},${longitude}`;
return this;
}

public setUserIdentity(userInfo: IUserInfo): EventBuilder;
public setUserIdentity(userInfo: UserInfo): EventBuilder;
public setUserIdentity(identity: string): EventBuilder;
public setUserIdentity(identity: string, name: string): EventBuilder;
public setUserIdentity(userInfoOrIdentity: IUserInfo | string, name?: string): EventBuilder {
const userInfo = typeof userInfoOrIdentity !== 'string' ? userInfoOrIdentity : { identity: userInfoOrIdentity, name };
public setUserIdentity(
userInfoOrIdentity: UserInfo | string,
name?: string,
): EventBuilder {
const userInfo = typeof userInfoOrIdentity !== "string"
? userInfoOrIdentity
: { identity: userInfoOrIdentity, name };
if (!userInfo || (!userInfo.identity && !userInfo.name)) {
return this;
}

this.setProperty('@user', userInfo);
this.setProperty("@user", userInfo);
return this;
}

Expand All @@ -105,9 +117,15 @@ export class EventBuilder {
* @param description The user's description of the event.
* @returns {EventBuilder}
*/
public setUserDescription(emailAddress: string, description: string): EventBuilder {
public setUserDescription(
emailAddress: string,
description: string,
): EventBuilder {
if (emailAddress && description) {
this.setProperty('@user_description', { email_address: emailAddress, description });
this.setProperty("@user_description", {
email_address: emailAddress,
description,
});
}

return this;
Expand All @@ -120,14 +138,17 @@ export class EventBuilder {
* @param title An optional title for the stacking information.
* @returns {EventBuilder}
*/
public setManualStackingInfo(signatureData: any, title?: string): EventBuilder {
public setManualStackingInfo(
signatureData: any,
title?: string,
): EventBuilder {
if (signatureData) {
const stack: IManualStackingInfo = { signature_data: signatureData };
const stack: ManualStackingInfo = { signature_data: signatureData };
if (title) {
stack.title = title;
}

this.setProperty('@stack', stack);
this.setProperty("@stack", stack);
}

return this;
Expand All @@ -139,7 +160,10 @@ export class EventBuilder {
* @param title An optional title for the stacking information.
* @returns {EventBuilder}
*/
public setManualStackingKey(manualStackingKey: string, title?: string): EventBuilder {
public setManualStackingKey(
manualStackingKey: string,
title?: string,
): EventBuilder {
if (manualStackingKey) {
const data = { ManualStackingKey: manualStackingKey };
this.setManualStackingInfo(data, title);
Expand Down Expand Up @@ -174,7 +198,12 @@ export class EventBuilder {
* @param maxDepth The max depth of the object to include.
* @param excludedPropertyNames Any property names that should be excluded.
*/
public setProperty(name: string, value: any, maxDepth?: number, excludedPropertyNames?: string[]): EventBuilder {
public setProperty(
name: string,
value: any,
maxDepth?: number,
excludedPropertyNames?: string[],
): EventBuilder {
if (!name || (value === undefined || value == null)) {
return this;
}
Expand All @@ -183,7 +212,13 @@ export class EventBuilder {
this.target.data = {};
}

const result = JSON.parse(stringify(value, this.client.config.dataExclusions.concat(excludedPropertyNames || []), maxDepth));
const result = JSON.parse(
stringify(
value,
this.client.config.dataExclusions.concat(excludedPropertyNames || []),
maxDepth,
),
);
if (!isEmpty(result)) {
this.target.data[name] = result;
}
Expand All @@ -193,15 +228,15 @@ export class EventBuilder {

public markAsCritical(critical: boolean): EventBuilder {
if (critical) {
this.addTags('Critical');
this.addTags("Critical");
}

return this;
}

public addRequestInfo(request: IRequestInfo): EventBuilder {
public addRequestInfo(request: RequestInfo): EventBuilder {
if (request) {
this.pluginContextData['@request'] = request;
this.pluginContextData["@request"] = request;
}

return this;
Expand All @@ -223,7 +258,8 @@ export class EventBuilder {
for (let index = 0; index < value.length; index++) {
const code = value.charCodeAt(index);
const isDigit = (code >= 48) && (code <= 57);
const isLetter = ((code >= 65) && (code <= 90)) || ((code >= 97) && (code <= 122));
const isLetter = ((code >= 65) && (code <= 90)) ||
((code >= 97) && (code <= 122));
const isMinus = code === 45;

if (!(isDigit || isLetter) && !isMinus) {
Expand Down
Loading