Skip to content

Commit

Permalink
sending only message to sentry (#77)
Browse files Browse the repository at this point in the history
Co-authored-by: Nagarjun Sanji <[email protected]>
  • Loading branch information
nagarjunsanji and Nagarjun Sanji authored Oct 22, 2024
1 parent f30c734 commit ae5a289
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 42 deletions.
6 changes: 3 additions & 3 deletions src/helpers/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosR
import { AuthHelper } from "./auth.helper";
import { ErrorHandler } from "./errorHandler";
import { Logger } from "./logger.helper";
import { Secrets } from "../constants";
import { MessageStatus, Secrets } from "../constants";
import * as Sentry from "@sentry/node";

export class ApiClient {
Expand Down Expand Up @@ -64,7 +64,7 @@ export class ApiClient {
} catch (error: any) {
span.setStatus(error.code);
span.end(new Date());
Sentry.captureException(error);
Sentry.captureException(error.message || MessageStatus.UNKNOWN);
throw error;
}
},
Expand All @@ -86,7 +86,7 @@ export class ApiClient {
} catch (error: any) {
span.setStatus(error.code);
span.end(new Date());
Sentry.captureException(error);
Sentry.captureException(error.message || MessageStatus.UNKNOWN);
throw error;
}
},
Expand Down
10 changes: 5 additions & 5 deletions src/helpers/debrickedService.helper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ApiHelper } from "./api.helper";
import { DependencyResponse, DependencyVulnerabilityWrapper, Repository, RequestParam } from "../types/index";
import { Logger } from "./logger.helper";
import { SecondService } from "../constants";
import { SentryHelper } from "./sentry.helper";
import { MessageStatus, SecondService } from "../constants";
import * as Sentry from "@sentry/node";

export class DebrickedServiceHelper {
private readonly apiHelper: ApiHelper;
Expand All @@ -27,7 +27,7 @@ export class DebrickedServiceHelper {
const response = await this.apiHelper.get(requestParam);
return response.repositories || [];
} catch (error: any) {
SentryHelper.captureException(error);
Sentry.captureException(error.message || MessageStatus.UNKNOWN);
throw error;
}
}
Expand All @@ -42,7 +42,7 @@ export class DebrickedServiceHelper {
try {
return await this.apiHelper.get(requestParam);
} catch (error: any) {
SentryHelper.captureException(error);
Sentry.captureException(error.message || MessageStatus.UNKNOWN);
throw error;
}
}
Expand All @@ -57,7 +57,7 @@ export class DebrickedServiceHelper {
try {
return await this.apiHelper.get(requestParam);
} catch (error: any) {
SentryHelper.captureException(error);
Sentry.captureException(error.message || MessageStatus.UNKNOWN);
throw error;
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/helpers/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ export class ErrorHandler {
) {}
public handleError(error: any, customErrorMessage?: string) {
const errorMessage = this.extractErrorMessage(error);
const errorStack = error instanceof Error ? error.stack : "";

this.logError(errorMessage, errorStack);
this.logError(errorMessage);
this.showUserErrorMessage(customErrorMessage ?? errorMessage);
this.sentryHelper.captureException(new Error(`Error Found :`, error));
}
Expand All @@ -27,11 +26,8 @@ export class ErrorHandler {
}
}

private logError(errorMessage: string, errorStack?: Error | string) {
private logError(errorMessage: string) {
this.logger.logError(`Error: ${errorMessage}`);
if (errorStack) {
this.logger.logException(errorStack);
}
}

private showUserErrorMessage(errorMessage: string) {
Expand Down
4 changes: 0 additions & 4 deletions src/helpers/logger.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,4 @@ export class Logger {
public static async logDebug(message: any) {
await this.logMessageByStatus(MessageStatus.DEBUG, JSON.stringify(message));
}

public static async logException(error: Error | string) {
await this.logMessageByStatus(MessageStatus.EXCEPTION, error);
}
}
2 changes: 1 addition & 1 deletion src/helpers/sentry.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class SentryHelper {
* @param error - The error object to capture.
*/
public static captureException(error: Error): void {
Sentry.captureException(error);
Sentry.captureException(error.message || MessageStatus.UNKNOWN);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/services/baseCommand.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import * as vscode from "vscode";
import { exec } from "child_process";
import { promisify } from "util";
import * as Sentry from "@sentry/node";

export class BaseCommandService {
constructor() {
Expand Down Expand Up @@ -183,7 +184,7 @@ export class BaseCommandService {
}
} catch (error: any) {
statusBarMessageHelper.showErrorMessage("Login Command Failed");
SentryHelper.captureException(new Error("Login Command Failed"));
Sentry.captureException(error.message || MessageStatus.UNKNOWN);
} finally {
statusBarMessageHelper.setStatusBarMessage(
StatusMessage.getStatusMessage(MessageStatus.FINISHED, DebrickedCommands.BASE_COMMAND.command),
Expand Down
13 changes: 6 additions & 7 deletions src/test/helpers/debrickedService.helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { sinon, expect } from "../setup";
import { DebrickedServiceHelper } from "../../helpers/debrickedService.helper";
import { ApiHelper } from "../../helpers/api.helper";
import { Logger } from "../../helpers/logger.helper";
import { SentryHelper } from "../../helpers/sentry.helper";
import * as Sentry from "@sentry/node";

describe("DebrickedServiceHelper", () => {
Expand Down Expand Up @@ -44,9 +43,9 @@ describe("DebrickedServiceHelper", () => {
const captureExceptionStub = sandbox.stub(Sentry, "captureException");
const error = new Error("Test error");

SentryHelper.captureException(error);
Sentry.captureException(error.message);

expect(captureExceptionStub.calledOnceWith(error)).to.be.true;
expect(captureExceptionStub.calledOnceWith(error.message)).to.be.true;
});
});

Expand All @@ -73,9 +72,9 @@ describe("DebrickedServiceHelper", () => {
const captureExceptionStub = sandbox.stub(Sentry, "captureException");
const error = new Error("Test error");

SentryHelper.captureException(error);
Sentry.captureException(error.message);

expect(captureExceptionStub.calledOnceWith(error)).to.be.true;
expect(captureExceptionStub.calledOnceWith(error.message)).to.be.true;
});
});

Expand All @@ -100,9 +99,9 @@ describe("DebrickedServiceHelper", () => {
const captureExceptionStub = sandbox.stub(Sentry, "captureException");
const error = new Error("Test error");

SentryHelper.captureException(error);
Sentry.captureException(error.message);

expect(captureExceptionStub.calledOnceWith(error)).to.be.true;
expect(captureExceptionStub.calledOnceWith(error.message)).to.be.true;
});
});
});
5 changes: 0 additions & 5 deletions src/test/helpers/errorHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ describe("ErrorHandler", () => {
sandbox = sinon.createSandbox();
loggerStub = {
logError: sandbox.stub(),
logException: sandbox.stub(),
} as any;
statusBarMessageHelperStub = {
showErrorMessage: sandbox.stub(),
Expand All @@ -38,7 +37,6 @@ describe("ErrorHandler", () => {
errorHandler.handleError(error);

expect(loggerStub.logError.calledOnceWith(`Error: ${error.message}`)).to.be.true;
expect(loggerStub.logException.calledOnceWith(error.stack)).to.be.true;
expect(statusBarMessageHelperStub.showErrorMessage.calledOnceWith(`${error.message}`)).to.be.true;
});

Expand All @@ -48,7 +46,6 @@ describe("ErrorHandler", () => {
errorHandler.handleError(error);

expect(loggerStub.logError.calledOnceWith(`Error: ${error}`)).to.be.true;
expect(loggerStub.logException.called).to.be.false;
expect(statusBarMessageHelperStub.showErrorMessage.calledOnceWith(`${error}`)).to.be.true;
});

Expand All @@ -58,7 +55,6 @@ describe("ErrorHandler", () => {
errorHandler.handleError(error);

expect(loggerStub.logError.calledOnceWith("Error: An unknown error occurred")).to.be.true;
expect(loggerStub.logException.called).to.be.false;
expect(statusBarMessageHelperStub.showErrorMessage.calledOnceWith("An unknown error occurred")).to.be.true;
});
});
Expand All @@ -76,7 +72,6 @@ describe("ErrorHandler", () => {
uncaughtExceptionHandler(error);
// expect(loggerStub.logError.calledOnceWith(`Uncaught Exception: ${error}`)).to.be.true;

expect(loggerStub.logException.calledOnceWith(error.stack)).to.be.true;
expect(statusBarMessageHelperStub.showErrorMessage.calledOnceWith(`${error.message}`)).to.be.true;
});

Expand Down
9 changes: 0 additions & 9 deletions src/test/helpers/logger.helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,4 @@ describe("Logger", () => {
expect(logMessageByStatusStub.calledWith(MessageStatus.DEBUG, JSON.stringify(message))).to.be.true;
});
});

describe("logException", () => {
it("should log exception message", async () => {
const error = new Error("exception message");
const logMessageByStatusStub = sandbox.stub(Logger, "logMessageByStatus");
await Logger.logException(error);
expect(logMessageByStatusStub.calledWith(MessageStatus.EXCEPTION, error)).to.be.true;
});
});
});
2 changes: 1 addition & 1 deletion src/test/helpers/sentry.helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("SentryHelper", () => {

SentryHelper.captureException(error);

expect(captureExceptionStub.calledOnceWith(error)).to.be.true;
expect(captureExceptionStub.calledOnceWith(error.message)).to.be.true;
});

it("should capture a custom message and send it to Sentry", () => {
Expand Down

0 comments on commit ae5a289

Please sign in to comment.