Some checks failed
Build and deploy / Validate source (push) Successful in 6s
Build and deploy / Publish images (push) Has been cancelled
Build and deploy / Secret scan and release gate (push) Has been cancelled
Build and deploy / Integration suite on a real stack (push) Has been cancelled
Until now only failed API calls and uncaught exceptions reached Sentry. An error the page caught and showed, like the folders' "A última tentativa falhou" Marcus photographed on 16/11, or an upload that failed on the Site, never did. Now every red message in the Kanban, the Site's checkout, the customer's account page and every failed upload is sent, one issue per kind of message; the same text an API error already sent, or a 401 / restart 503 it skips on purpose, is not sent twice. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
160 lines
6.7 KiB
JavaScript
160 lines
6.7 KiB
JavaScript
/* Sentry for the Site and the Kanban, used only when the DSN arrives from the
|
|
API. It loads after web/vendor/sentry.min.js and never fetches anything
|
|
itself; DSNs are public by design, so an empty one leaves everything off.
|
|
|
|
beforeSend follows the server (app/observability.py): no cookies, credential
|
|
headers, request bodies or signed/token query strings, and CPF/CNPJ and
|
|
e-mails replaced by [removido]. */
|
|
(function () {
|
|
const EMAIL = /[^\s@]+@[^\s@]+\.[A-Za-z]{2,}/g;
|
|
const DOCUMENT = /(?<!\d)(?:\d{2}\.?\d{3}\.?\d{3}\/?\d{4}-?\d{2}|\d{3}\.?\d{3}\.?\d{3}-?\d{2})(?!\d)/g;
|
|
// What changes from one API error to the next of the same kind: the ids of
|
|
// the files, quotes and orders, the numbers and the file names.
|
|
const UUID = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi;
|
|
const FILE_NAME = /[^\s"'<>:,;()[\]{}]+\.(?:png|jpe?g|pdf|tiff?|psd|psb|ai|cdr|webp|zip)\b/gi;
|
|
const NUMBER = /\d+/g;
|
|
const SENSITIVE_PARAM = /token|signature|x-amz/i;
|
|
const SENSITIVE_HEADER = /^(cookie|authorization|x-.*token.*)$/i;
|
|
const REDACTED = '[removido]';
|
|
let started = false;
|
|
let app = '';
|
|
|
|
const clean = value => String(value == null ? '' : value)
|
|
.replace(EMAIL, REDACTED).replace(DOCUMENT, REDACTED);
|
|
|
|
function scrubQuery(query) {
|
|
if (!query) return query;
|
|
return String(query).split('&').filter(part => {
|
|
let key = part.split('=')[0] || '';
|
|
try { key = decodeURIComponent(key.replace(/\+/g, ' ')); } catch (error) { /* keep the raw key */ }
|
|
return !SENSITIVE_PARAM.test(key);
|
|
}).join('&');
|
|
}
|
|
|
|
function scrubText(value) {
|
|
if (typeof value !== 'string') return value;
|
|
value = clean(value);
|
|
const at = value.indexOf('?');
|
|
return at < 0 ? value : value.slice(0, at + 1) + scrubQuery(value.slice(at + 1));
|
|
}
|
|
|
|
function scrubUrl(url) {
|
|
if (typeof url !== 'string') return url;
|
|
const at = url.indexOf('?');
|
|
return at < 0 ? url : url.slice(0, at + 1) + scrubQuery(url.slice(at + 1));
|
|
}
|
|
|
|
function scrubValue(value) {
|
|
if (typeof value === 'string') return scrubText(value);
|
|
if (Array.isArray(value)) return value.map(scrubValue);
|
|
if (value && typeof value === 'object') {
|
|
const out = {};
|
|
for (const [key, item] of Object.entries(value)) out[key] = scrubValue(item);
|
|
return out;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function scrubRequest(request) {
|
|
if (!request || typeof request !== 'object') return;
|
|
delete request.cookies;
|
|
delete request.data;
|
|
delete request.body;
|
|
if (request.headers && typeof request.headers === 'object') {
|
|
for (const key of Object.keys(request.headers)) {
|
|
if (SENSITIVE_HEADER.test(key)) delete request.headers[key];
|
|
}
|
|
}
|
|
if (request.query_string) request.query_string = scrubQuery(request.query_string);
|
|
if (request.url) request.url = scrubUrl(request.url);
|
|
}
|
|
|
|
function scrub(event) {
|
|
if (!event || typeof event !== 'object') return event;
|
|
scrubRequest(event.request);
|
|
if (typeof event.message === 'string') event.message = scrubText(event.message);
|
|
if (event.logentry && typeof event.logentry.message === 'string')
|
|
event.logentry.message = scrubText(event.logentry.message);
|
|
if (event.exception && Array.isArray(event.exception.values)) {
|
|
for (const value of event.exception.values) {
|
|
if (value && typeof value.value === 'string') value.value = scrubText(value.value);
|
|
}
|
|
}
|
|
let breadcrumbs = event.breadcrumbs;
|
|
if (breadcrumbs && !Array.isArray(breadcrumbs)) breadcrumbs = breadcrumbs.values;
|
|
if (Array.isArray(breadcrumbs)) {
|
|
for (const crumb of breadcrumbs) {
|
|
if (!crumb || typeof crumb !== 'object') continue;
|
|
if (typeof crumb.message === 'string') crumb.message = scrubText(crumb.message);
|
|
if (crumb.data) crumb.data = scrubValue(crumb.data);
|
|
}
|
|
}
|
|
if (event.extra) event.extra = scrubValue(event.extra);
|
|
if (event.user) event.user = scrubValue(event.user);
|
|
return event;
|
|
}
|
|
|
|
function start(dsn, component) {
|
|
if (started || !dsn || typeof Sentry === 'undefined') return;
|
|
started = true;
|
|
app = component || '';
|
|
const options = {dsn, sendDefaultPii: false, beforeSend: scrub};
|
|
if (window.dtfEnvironment) options.environment = window.dtfEnvironment;
|
|
if (window.dtfRelease) options.release = window.dtfRelease;
|
|
try {
|
|
Sentry.init(options);
|
|
Sentry.setTag('app', app);
|
|
} catch (error) {
|
|
started = false;
|
|
}
|
|
}
|
|
|
|
const normalizedPath = value => clean(String(value == null ? '' : value)
|
|
.replace(UUID, ':id').replace(NUMBER, ':id'));
|
|
const normalizedMessage = value => clean(String(value == null ? '' : value)
|
|
.replace(UUID, ':id').replace(FILE_NAME, ':arquivo').replace(NUMBER, ':id'));
|
|
|
|
// What apiError already sent, so the same text shown in red is not sent twice.
|
|
const recent = new Map();
|
|
function apiError(path, status, message, detail) {
|
|
// Recorded first: a skipped 401 or restart 503 stays out of uiError too.
|
|
recent.set(String(message), Date.now());
|
|
if (!started || status === 401 || typeof Sentry === 'undefined') return;
|
|
// A restart's few seconds without the database (app/app.py): not a bug.
|
|
if (status === 503 && /reiniciando/.test(String(message))) return;
|
|
try {
|
|
// One issue per kind of API error: without this Sentry groups them all
|
|
// by apiError and DTF-WEB-1 mixes a 422 for 66 files with a 422 for
|
|
// 96 DPI. The title names the same kind as the fingerprint.
|
|
const kindPath = normalizedPath(path);
|
|
const kindMessage = normalizedMessage(message);
|
|
const extra = {path, status, page: location.pathname};
|
|
if (status === 422 && detail) extra.detail = detail;
|
|
Sentry.captureMessage('API ' + status + ' ' + kindPath + ': ' + kindMessage,
|
|
{level: 'error', tags: {app}, extra,
|
|
fingerprint: ['api-error', String(status), kindPath, kindMessage]});
|
|
} catch (error) {
|
|
// Reporting an error must never break the page.
|
|
}
|
|
}
|
|
|
|
// Every error the page shows the user in red, also the ones it caught and
|
|
// handled itself (a folder it could not write, an upload that failed): the
|
|
// team sees them in Sentry before anyone sends a screenshot.
|
|
function uiError(text, where) {
|
|
if (!started || !text || typeof Sentry === 'undefined') return;
|
|
const said = recent.get(String(text));
|
|
if (said && Date.now() - said < 15000) return;
|
|
try {
|
|
const kind = normalizedMessage(text);
|
|
Sentry.captureMessage('Aviso ' + (where || app) + ': ' + kind,
|
|
{level: 'error', tags: {app, aviso: where || app}, extra: {page: location.pathname},
|
|
fingerprint: ['ui-error', where || app, kind]});
|
|
} catch (error) {
|
|
// Reporting an error must never break the page.
|
|
}
|
|
}
|
|
|
|
window.dtfObservability = {start, apiError, uiError, scrub};
|
|
})();
|