Skip to content
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 packages/app/error-utils/biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"$schema": "../../../node_modules/@biomejs/biome/configuration_schema.json",
"extends": [
"../../../node_modules/@lokalise/biome-config/configs/biome-base.jsonc",
"../../../node_modules/@lokalise/biome-config/configs/biome-package.jsonc"
"../../../node_modules/@lokalise/biome-config/configs/biome-package.jsonc",
"../../../node_modules/@lokalise/biome-config/configs/biome-esm.jsonc"
]
}
15 changes: 8 additions & 7 deletions packages/app/error-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
"version": "2.2.0",
"license": "Apache-2.0",
"files": ["dist/**", "LICENSE.md", "README.md"],
"type": "commonjs",
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": "./dist/index.js",
"./package.json": "./package.json"
},
"homepage": "https://github.com/lokalise/shared-ts-libs",
"repository": {
"type": "git",
"url": "git://github.com/lokalise/shared-ts-libs.git"
},
"scripts": {
"build": "rimraf dist && tsc",
"clean": "rimraf dist .eslintcache",
"lint": "biome check . && tsc --project tsconfig.lint.json --noEmit",
"build": "rimraf dist && tsc --project tsconfig.build.json",
"lint": "biome check . && tsc",
"lint:fix": "biome check --write",
"test:ci": "vitest run --coverage",
"prepublishOnly": "npm run build",
Expand All @@ -31,11 +33,10 @@
"@biomejs/biome": "^1.9.4",
"@lokalise/biome-config": "^1.5.0",
"@lokalise/node-core": "^13.1.0",
"@lokalise/package-vite-config": "latest",
"@lokalise/tsconfig": "^1.0.2",
"@vitest/coverage-v8": "^3.0.7",
"rimraf": "^6.0.1",
"typescript": "5.8.2",
"vite": "6.2.1",
"vitest": "^3.0.7"
}
}
61 changes: 31 additions & 30 deletions packages/app/error-utils/src/bugsnag.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import Bugsnag from '@bugsnag/js'
import { type FreeformRecord, InternalError, PublicNonRecoverableError } from '@lokalise/node-core'
import { describe, expect, it, vi } from 'vitest'
import { reportErrorToBugsnag } from './bugsnag.js'

import { reportErrorToBugsnag } from './bugsnag'
const BugsnagClient = Bugsnag.default

describe('bugsnag', () => {
describe('reportErrorToBugsnag', () => {
it('not started', () => {
// Given
const startSpy = vi.spyOn(Bugsnag, 'isStarted').mockReturnValue(false)
const notifySpy = vi.spyOn(Bugsnag, 'notify')
const startSpy = vi.spyOn(BugsnagClient, 'isStarted').mockReturnValue(false)
const notifySpy = vi.spyOn(BugsnagClient, 'notify')

// When
reportErrorToBugsnag({ error: new Error('test') })
Expand All @@ -21,42 +22,42 @@ describe('bugsnag', () => {

it('using Error', async () => {
// Given
vi.spyOn(Bugsnag, 'isStarted').mockReturnValue(true)
const notifySpy = vi.spyOn(Bugsnag, 'notify').mockReturnValue(undefined)
vi.spyOn(BugsnagClient, 'isStarted').mockReturnValue(true)
const notifySpy = vi.spyOn(BugsnagClient, 'notify').mockReturnValue(undefined)

// When
reportErrorToBugsnag({ error: new Error('test') })

// Then
expect(notifySpy).toHaveBeenCalled()

const callback = notifySpy.mock.calls[0][1]
const event = { addMetadata: (_, __) => undefined } as any
await callback(event, () => {})
const callback = notifySpy.mock.calls[0]![1]
const event = { addMetadata: () => undefined } as any
await callback!(event, () => {})
expect(event).toMatchObject({ severity: 'error', unhandled: true })
})

it('custom severity and unhandled', async () => {
// Given
vi.spyOn(Bugsnag, 'isStarted').mockReturnValue(true)
const notifySpy = vi.spyOn(Bugsnag, 'notify').mockReturnValue(undefined)
vi.spyOn(BugsnagClient, 'isStarted').mockReturnValue(true)
const notifySpy = vi.spyOn(BugsnagClient, 'notify').mockReturnValue(undefined)

// When
reportErrorToBugsnag({ error: new Error('test'), severity: 'info', unhandled: false })

// Then
expect(notifySpy).toHaveBeenCalled()

const callback = notifySpy.mock.calls[0][1]
const event = { addMetadata: (_, __) => undefined } as any
await callback(event, () => {})
const callback = notifySpy.mock.calls[0]![1]
const event = { addMetadata: () => undefined } as any
await callback!(event, () => {})
expect(event).toMatchObject({ severity: 'info', unhandled: false })
})

it('internal error', async () => {
// Given
vi.spyOn(Bugsnag, 'isStarted').mockReturnValue(true)
const notifySpy = vi.spyOn(Bugsnag, 'notify').mockReturnValue(undefined)
vi.spyOn(BugsnagClient, 'isStarted').mockReturnValue(true)
const notifySpy = vi.spyOn(BugsnagClient, 'notify').mockReturnValue(undefined)

// When
reportErrorToBugsnag({
Expand All @@ -71,15 +72,15 @@ describe('bugsnag', () => {
// Then
expect(notifySpy).toHaveBeenCalled()

const callback = notifySpy.mock.calls[0][1]
let context = {}
const callback = notifySpy.mock.calls[0]![1]
let context: unknown = {}
const event = {
addMetadata: (key, obj) => {
addMetadata: (key: unknown, obj: unknown) => {
if (key === 'Context') context = obj
else throw new Error('wrong key')
},
} as any
await callback(event, () => {})
await callback!(event, () => {})
expect(event).toMatchObject({ severity: 'error', unhandled: true })
expect(context).toMatchObject({
good: 'bye',
Expand All @@ -90,8 +91,8 @@ describe('bugsnag', () => {

it('public non recoverable error', async () => {
// Given
vi.spyOn(Bugsnag, 'isStarted').mockReturnValue(true)
const notifySpy = vi.spyOn(Bugsnag, 'notify').mockReturnValue(undefined)
vi.spyOn(BugsnagClient, 'isStarted').mockReturnValue(true)
const notifySpy = vi.spyOn(BugsnagClient, 'notify').mockReturnValue(undefined)

// When
reportErrorToBugsnag({
Expand All @@ -106,10 +107,10 @@ describe('bugsnag', () => {
// Then
expect(notifySpy).toHaveBeenCalled()

const callback = notifySpy.mock.calls[0][1]
let context = {}
const callback = notifySpy.mock.calls[0]![1]!
let context: unknown = {}
const event = {
addMetadata: (key, obj) => {
addMetadata: (key: unknown, obj: unknown) => {
if (key === 'Context') context = obj
else throw new Error('wrong key')
},
Expand All @@ -125,8 +126,8 @@ describe('bugsnag', () => {

it('unknown error with details field', async () => {
// Given
vi.spyOn(Bugsnag, 'isStarted').mockReturnValue(true)
const notifySpy = vi.spyOn(Bugsnag, 'notify').mockReturnValue(undefined)
vi.spyOn(BugsnagClient, 'isStarted').mockReturnValue(true)
const notifySpy = vi.spyOn(BugsnagClient, 'notify').mockReturnValue(undefined)

// When
reportErrorToBugsnag({
Expand All @@ -137,15 +138,15 @@ describe('bugsnag', () => {
// Then
expect(notifySpy).toHaveBeenCalled()

const callback = notifySpy.mock.calls[0][1]
let context = {}
const callback = notifySpy.mock.calls[0]![1]
let context: unknown = {}
const event = {
addMetadata: (key, obj) => {
addMetadata: (key: unknown, obj: unknown) => {
if (key === 'Context') context = obj
else throw new Error('wrong key')
},
} as any
await callback(event, () => {})
await callback!(event, () => {})
expect(event).toMatchObject({ severity: 'error', unhandled: true })
expect(context).toMatchObject({
good: 'bye',
Expand Down
11 changes: 6 additions & 5 deletions packages/app/error-utils/src/bugsnag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { isError, isInternalError, isPublicNonRecoverableError } from '@lokalise
const hasDetails = (
error: NotifiableError,
): error is NotifiableError & { details: FreeformRecord } => isError(error) && 'details' in error
const BugsnagClient = Bugsnag.default

export type Severity = Event['severity']

Expand All @@ -23,8 +24,8 @@ export const reportErrorToBugsnag = ({
unhandled = true,
context,
}: ErrorReport) =>
Bugsnag.isStarted() &&
Bugsnag.notify(error, (event) => {
BugsnagClient.isStarted() &&
BugsnagClient.notify(error, (event) => {
let computedContext = { ...(context ?? {}) }
if (isPublicNonRecoverableError(error) || isInternalError(error)) {
computedContext = {
Expand All @@ -49,13 +50,13 @@ export const reportErrorToBugsnag = ({
})

export const startBugsnag = (config: NodeConfig) => {
if (!Bugsnag.isStarted()) {
Bugsnag.start(config)
if (!BugsnagClient.isStarted()) {
BugsnagClient.start(config)
}
}

export const addFeatureFlag = (name: string, variant: string | null) => {
Bugsnag.addFeatureFlag(name, variant)
BugsnagClient.addFeatureFlag(name, variant)
}

export const bugsnagErrorReporter: ErrorReporter = {
Expand Down
2 changes: 1 addition & 1 deletion packages/app/error-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './bugsnag'
export * from './bugsnag.js'
5 changes: 5 additions & 0 deletions packages/app/error-utils/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": ["./tsconfig.json", "@lokalise/tsconfig/build-public-lib"],
"include": ["src/**/*"],
"exclude": ["src/**/*.spec.ts", "src/**/*.test.ts"]
}
27 changes: 2 additions & 25 deletions packages/app/error-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,4 @@
{
"compilerOptions": {
"outDir": "dist",
"module": "CommonJS",
"target": "ES2022",
"lib": ["ES2022", "dom"],
"sourceMap": false,
"declaration": true,
"declarationMap": false,
"types": ["vitest/globals"],
"skipLibCheck": true,
"strict": true,
"moduleResolution": "node",
"noUnusedLocals": false,
"noUnusedParameters": false,
"noFallthroughCasesInSwitch": true,
"strictNullChecks": true,
"importHelpers": true,
"baseUrl": "./",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true
},
"include": ["src/**/*"],
"exclude": ["src/**/*.spec.ts", "src/**/*.test.ts"]
"extends": "@lokalise/tsconfig/tsc",
"include": ["src/**/*", "vitest.config.ts"]
}
5 changes: 0 additions & 5 deletions packages/app/error-utils/tsconfig.lint.json

This file was deleted.

Loading