-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(frontend): detect preprod from hostname (#1103)
- Loading branch information
1 parent
6dc24b3
commit 91d01b6
Showing
5 changed files
with
101 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/code-du-travail-frontend/src/__tests__/sentry.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Sentry from "@sentry/browser"; | ||
import { initializeSentry, notifySentry } from "../sentry"; | ||
|
||
jest.mock("@sentry/browser", () => ({ | ||
captureMessage: jest.fn(), | ||
init: jest.fn(), | ||
withScope: jest.fn(cb => cb({ setTag: jest.fn() })) | ||
})); | ||
|
||
test("should initialize sentry in production mode", () => { | ||
initializeSentry(); | ||
|
||
expect(Sentry.init).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
debug: false, | ||
dsn: "https://[email protected]/n", | ||
environment: "production", | ||
release: "vX.Y.Z" | ||
}) | ||
); | ||
}); | ||
|
||
test("should initialize sentry in pre-production mode", () => { | ||
Object.defineProperty(window, "location", { | ||
value: new URL("https://v11-22-33.code.travail.gouv.fr/") | ||
}); | ||
|
||
initializeSentry(); | ||
|
||
expect(Sentry.init).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
debug: true, | ||
dsn: "https://[email protected]/n", | ||
environment: "preproduction", | ||
release: "vX.Y.Z" | ||
}) | ||
); | ||
}); | ||
|
||
test("should notify sentry", () => { | ||
notifySentry(418, "I'm a teapot"); | ||
|
||
expect(Sentry.captureMessage).toHaveBeenCalledWith( | ||
"Error 418 - I'm a teapot", | ||
"error" | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Sentry from "@sentry/browser"; | ||
import getConfig from "next/config"; | ||
|
||
const { | ||
publicRuntimeConfig: { SENTRY_PUBLIC_DSN, PACKAGE_VERSION } | ||
} = getConfig(); | ||
|
||
const isEnable = typeof window !== "undefined" && SENTRY_PUBLIC_DSN; | ||
|
||
export function initializeSentry() { | ||
if (!isEnable) { | ||
return; | ||
} | ||
|
||
const packageVersion = PACKAGE_VERSION || ""; | ||
// NOTE(douglasduteil): is pre production if we can find the version in the url | ||
// All "http://<version>.code-du-travail-numerique.[...].fr" are preprod | ||
// "http://code-du-travail-numerique.[...].fr" is prod | ||
const isPreProduction = | ||
packageVersion && /^v\d+-\d+-\d+/.test(location.hostname); | ||
const environment = isPreProduction ? "preproduction" : "production"; | ||
Sentry.init({ | ||
dsn: SENTRY_PUBLIC_DSN, | ||
debug: isPreProduction, | ||
environment, | ||
release: packageVersion | ||
}); | ||
} | ||
|
||
export function notifySentry(statusCode, message) { | ||
if (!isEnable) { | ||
return; | ||
} | ||
|
||
Sentry.withScope(scope => { | ||
scope.setTag(`ssr`, false); | ||
Sentry.captureMessage( | ||
`Error ${statusCode}${message ? ` - ${message}` : ""}`, | ||
"error" | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,13 @@ import "jest-styled-components"; | |
|
||
jest.mock("next-server/config", () => () => ({ | ||
publicRuntimeConfig: { | ||
API_URL: "api.url", | ||
API_SIRET2IDCC_URL: "siret2idcc.url", | ||
API_DILA2SQL_URL: "https://api.dila2sql.num.social.gouv.fr/v1", | ||
SUGGEST_URL: "suggest.url/suggest", | ||
API_ADDRESS: "addresse-api.data", | ||
PACKAGE_VERSION: "vX.Y.Z" | ||
API_DILA2SQL_URL: "https://api.dila2sql.num.social.gouv.fr/v1", | ||
API_SIRET2IDCC_URL: "siret2idcc.url", | ||
API_URL: "api.url", | ||
PACKAGE_VERSION: "vX.Y.Z", | ||
SENTRY_PUBLIC_DSN: "https://[email protected]/n", | ||
SUGGEST_URL: "suggest.url/suggest" | ||
} | ||
})); | ||
|
||
|