Skip to content

Commit

Permalink
fix(frontend): detect preprod from hostname (#1103)
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasduteil authored Aug 21, 2019
1 parent 6dc24b3 commit 91d01b6
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 44 deletions.
7 changes: 3 additions & 4 deletions packages/code-du-travail-frontend/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import * as Sentry from "@sentry/browser";
import ErrorPage from "./_error";

import { initPiwik } from "../src/piwik";
import { initializeSentry } from "../src/sentry";

const {
publicRuntimeConfig: { PIWIK_URL, PIWIK_SITE_ID, SENTRY_PUBLIC_DSN }
publicRuntimeConfig: { PIWIK_URL, PIWIK_SITE_ID }
} = getConfig();

if (typeof window !== "undefined" && SENTRY_PUBLIC_DSN) {
Sentry.init({ dsn: SENTRY_PUBLIC_DSN, debug: true });
}
initializeSentry();

export default class MyApp extends App {
// HACK @lionelb from https://github.com/zeit/next.js/issues/4687#issuecomment-432608667
Expand Down
38 changes: 3 additions & 35 deletions packages/code-du-travail-frontend/pages/_error.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,11 @@
import React from "react";
import Link from "next/link";
import getConfig from "next/config";
import styled from "styled-components";
import { Section, theme } from "@cdt/ui";
import { PageLayout } from "../src/layout/PageLayout";
const Sentry = require("@sentry/browser");
import { initializeSentry, notifySentry } from "../src/sentry";

const {
publicRuntimeConfig: { SENTRY_PUBLIC_DSN, PACKAGE_VERSION }
} = getConfig();

if (typeof window !== "undefined" && SENTRY_PUBLIC_DSN) {
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 && location.href.indexOf(packageVersion) >= 0;
const environment = isPreProduction ? "preproduction" : "production";
Sentry.init({
dsn: SENTRY_PUBLIC_DSN,
debug: true,
environment,
release: packageVersion
});
}

const notifySentry = (statusCode, message) => {
if (typeof window === "undefined") {
return;
}
Sentry.withScope(scope => {
scope.setTag(`ssr`, false);
Sentry.captureMessage(
`Error ${statusCode}${message ? ` - ${message}` : ""}`,
"error"
);
});
};
initializeSentry();

export default class Error extends React.Component {
static async getInitialProps({ res, err }) {
Expand All @@ -47,7 +15,7 @@ export default class Error extends React.Component {

componentDidMount() {
const { statusCode, message } = this.props;
if (statusCode && statusCode > 200) {
if (statusCode && statusCode >= 400) {
notifySentry(statusCode, message);
}
}
Expand Down
47 changes: 47 additions & 0 deletions packages/code-du-travail-frontend/src/__tests__/sentry.test.js
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"
);
});
42 changes: 42 additions & 0 deletions packages/code-du-travail-frontend/src/sentry.js
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"
);
});
}
11 changes: 6 additions & 5 deletions packages/code-du-travail-frontend/test/jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}));

Expand Down

0 comments on commit 91d01b6

Please sign in to comment.