Skip to content

Commit

Permalink
Error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas.jaseliunas committed Jan 19, 2024
1 parent f7f76bd commit c39966b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
13 changes: 12 additions & 1 deletion src/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export interface NostoClient {
type: "serp" | "autocomplete" | "category",
hit: { url?: string; keyword?: string }
): void
recordSearchSubmit(query: string): void
recordSearchSubmit(query: string): void,
captureError: (error: unknown, reporter: string, level: 'debug' | 'info' | 'warn' | 'error') => void
}

/**
Expand All @@ -82,6 +83,16 @@ export function getNostoClient(): PromiseLike<NostoClient> {
})
}

export function logAndCaptureError(error: unknown, level: 'debug' | 'info' | 'warn' | 'error') {
getNostoClient().then(api => {
api.captureError(error, 'nostoAutocomplete', level)
})
const acceptedLogs = ['debug', 'info', 'warn', 'error']
if (level in acceptedLogs) {
console[level](error)
}
}

/**
* @group Nosto Client
* @category Recommendation Types
Expand Down
9 changes: 5 additions & 4 deletions src/autocomplete.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getNostoClient } from "./api/client"
import { getNostoClient, logAndCaptureError } from "./api/client"
import { AutocompleteConfig, getDefaultConfig } from "./config"
import { Dropdown, createDropdown, parseHit } from "./utils/dropdown"
import { DefaultState, StateActions, getStateActions } from "./utils/state"
Expand Down Expand Up @@ -260,12 +260,13 @@ function createInputDropdown<State>({
: findAll(config.dropdownSelector, HTMLElement)

if (dropdownElements.length === 0) {
console.error(`No dropdown element found for input ${input}`)
const noElementsError = `No dropdown element found for input ${input}`
logAndCaptureError(new Error(noElementsError), "error")
return
} else if (dropdownElements.length > 1) {
console.error(
logAndCaptureError(new Error(
`Multiple dropdown elements found for input ${input}, using the first element`
)
), "error")
}

const dropdownElement = dropdownElements[0]
Expand Down
4 changes: 3 additions & 1 deletion src/utils/dropdown.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { logAndCaptureError } from "../api/client"
import { AnyPromise } from "./promise"

type OnClickBindings<State> = {
Expand Down Expand Up @@ -270,7 +271,8 @@ export function parseHit(hit: string): Hit {
const parsedHit: Hit | undefined | null = JSON.parse(hit)
return parsedHit ?? {}
} catch (error) {
console.warn("Could not parse hit", error)
logAndCaptureError(error, "warn")
console.warn("Could not parse hit")
return {}
}
}
9 changes: 6 additions & 3 deletions src/utils/ga.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { logAndCaptureError } from "../api/client"
import { AutocompleteConfig, defaultGaConfig } from "../config"

const localStorageKey = "nostoAutocomplete:gaEvent"
Expand Down Expand Up @@ -68,7 +69,8 @@ export function trackGaPageView(options?: {
trackers[0]?.send("pageview", url.pathname + url.search)
}
} catch (error) {
console.log("Could not send pageview to GA", error)
logAndCaptureError(error, "warn")
console.warn("Could not send pageview to GA")
}
}
}
Expand Down Expand Up @@ -104,7 +106,8 @@ export const getGaTrackUrl = <State>(
window.location.origin
).toString()
} catch (error) {
console.log("Could not create track url", error)
logAndCaptureError(error, "warn")
console.warn("Could not create track url")
return undefined
}
}
Expand All @@ -125,7 +128,7 @@ function consumeLocalStorageEvent(): void {
}
trackGaPageView(event)
} catch (e) {
console.warn(e)
logAndCaptureError(e, "warn")
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/utils/history.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { logAndCaptureError } from "../api/client"
import { DefaultState } from "./state"

type Items = NonNullable<DefaultState["history"]>
Expand All @@ -12,7 +13,8 @@ export function createHistory(size: number) {
JSON.parse(localStorage.getItem(localStorageKey) ?? "[]") ?? []
)
} catch (err) {
console.error("Could not get history items.", err)
logAndCaptureError(err, "error")
console.error("Could not get history items.")
return [] as Items
}
}
Expand All @@ -21,7 +23,8 @@ export function createHistory(size: number) {
try {
localStorage.setItem(localStorageKey, JSON.stringify(data))
} catch (err) {
console.error("Could not set history items.", err)
logAndCaptureError(err, "error")
console.error("Could not set history items.")
}
}

Expand Down

0 comments on commit c39966b

Please sign in to comment.