Skip to content

Commit

Permalink
chore: Added sentry call for 500 API errors
Browse files Browse the repository at this point in the history
  • Loading branch information
devendrafyle committed Jan 8, 2025
1 parent f890298 commit b9a0e33
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
40 changes: 40 additions & 0 deletions src/app/core/interceptors/httpInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { SecureStorageService } from '../services/secure-storage.service';
import { StorageService } from '../services/storage.service';
import { TokenService } from '../services/token.service';
import { UserEventService } from '../services/user-event.service';
import * as Sentry from '@sentry/angular';

@Injectable()
export class HttpConfigInterceptor implements HttpInterceptor {
Expand Down Expand Up @@ -153,6 +154,7 @@ export class HttpConfigInterceptor implements HttpInterceptor {
return next.handle(request).pipe(
catchError((error) => {
if (error instanceof HttpErrorResponse) {
this.handleSentryError(error, request);
if (this.expiringSoon(token)) {
return from(this.refreshAccessToken()).pipe(
mergeMap((newToken) => {
Expand All @@ -177,6 +179,44 @@ export class HttpConfigInterceptor implements HttpInterceptor {
})
);
}

private handleSentryError(error: HttpErrorResponse, request: HttpRequest<string>): void {
if (error.status >= 500) {
const errorObject = new Error(`API ${error.status} Error: ${error.message || 'Server error'}`);

Object.assign(errorObject, {
status: error.status,
statusText: error.statusText,
});

Sentry.captureException(errorObject, {
tags: {
errorType: 'API_500',
apiEndpoint: error.url,
},
extra: {
requestUrl: request.url,
requestMethod: request.method,
requestHeaders: request.headers,
requestBody: request.body,
responseStatus: error.status,
responseStatusText: error.statusText,
responseData: error.error,
},
});

Sentry.addBreadcrumb({
category: 'api',
message: `API call failed with status ${error.status}`,
level: 'error',
data: {
url: request.url,
method: request.method,
status: error.status,
},
});
}
}
}

export class CustomEncoder implements HttpParameterCodec {
Expand Down
33 changes: 30 additions & 3 deletions src/app/fyle/my-view-report/my-view-report.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { ExtendedComment } from 'src/app/core/models/platform/v1/extended-commen
import { Comment } from 'src/app/core/models/platform/v1/comment.model';
import { ExpenseTransactionStatus } from 'src/app/core/enums/platform/v1/expense-transaction-status.enum';
import * as Sentry from '@sentry/angular';
import { HttpErrorResponse } from '@angular/common/http';

@Component({
selector: 'app-my-view-report',
Expand Down Expand Up @@ -455,12 +456,38 @@ export class MyViewReportPage {
});
this.trackingService.showToastMessage({ ToastContent: message });
},
error: (error) => {
// Capture error with additional details in Sentry
Sentry.captureException(error, {
error: (error: HttpErrorResponse) => {
const errorObject = new Error(`Report Submission Failed: ${error.message || 'Unknown error'}`);
if (error.status) {
Object.assign(errorObject, {
status: error.status,
statusText: error.statusText,
});
}

Sentry.addBreadcrumb({
category: 'reports',
message: 'Failed to submit report',
level: 'error',
data: {
reportId: this.reportId,
errorStatus: error.status,
errorMessage: error.message,
},
});

Sentry.captureException(errorObject, {
tags: {
errorType: 'REPORT_SUBMISSION_FAILED',
reportId: this.reportId,
},
extra: {
reportId: this.reportId,
errorResponse: error,
context: {
component: 'ReportSubmissionComponent',
action: 'submitReport',
},
},
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,23 @@ export class CameraPreviewComponent implements OnInit, OnChanges {
stopCamera(): void {
//Stop camera only if it is in RUNNING state
if (this.cameraState === CameraState.RUNNING) {
const currentCameraState = this.cameraState;
this.cameraState = CameraState.STOPPING;
from(this.cameraPreviewService.stop()).subscribe(() => (this.cameraState = CameraState.STOPPED));

from(this.cameraPreviewService.stop()).subscribe({
next: () => {
this.cameraState = CameraState.STOPPED;
},
error: (error) => {
Sentry.captureException(error, {
extra: {
errorResponse: error,
currentCameraState,
cameraState: this.cameraState,
},
});
},
});
}
}

Expand Down

0 comments on commit b9a0e33

Please sign in to comment.