|
| 1 | +// Very happy integration that'll prepend and append very happy stick figure to the message |
| 2 | +class HappyIntegration { |
| 3 | + constructor() { |
| 4 | + this.name = "HappyIntegration"; |
| 5 | + } |
| 6 | + |
| 7 | + setupOnce() { |
| 8 | + Sentry.configureScope(scope => { |
| 9 | + scope.addEventProcessor(async event => { |
| 10 | + if (event.message === "Happy Message") { |
| 11 | + event.message = `\\o/ ${event.message} \\o/`; |
| 12 | + } |
| 13 | + return event; |
| 14 | + }); |
| 15 | + }); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +class HappyTransport extends Sentry.Transports.BaseTransport { |
| 20 | + captureEvent(event) { |
| 21 | + console.log(`This is the place where you'd implement your own sending logic. It'd get url: ${this.url} and an event itself:`, event); |
| 22 | + |
| 23 | + return { |
| 24 | + status: 'success' |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +Sentry.init({ |
| 30 | + // Client's DSN. |
| 31 | + dsn: "https://[email protected]/297378", |
| 32 | + // An array of strings or regexps that'll be used to ignore specific errors based on their type/message |
| 33 | + ignoreErrors: [/PickleRick_\d\d/, "RangeError"], |
| 34 | + // // An array of strings or regexps that'll be used to ignore specific errors based on their origin url |
| 35 | + blacklistUrls: ["external-lib.js"], |
| 36 | + // // An array of strings or regexps that'll be used to allow specific errors based on their origin url |
| 37 | + whitelistUrls: ["http://localhost:5000", "https://browser.sentry-cdn"], |
| 38 | + // // Debug mode with valuable initialization/lifecycle informations. |
| 39 | + debug: true, |
| 40 | + // Whether SDK should be enabled or not. |
| 41 | + enabled: true, |
| 42 | + // Custom integrations callback |
| 43 | + integrations(integrations) { |
| 44 | + return [new HappyIntegration(), ...integrations]; |
| 45 | + }, |
| 46 | + // A release identifier. |
| 47 | + release: "1537345109360", |
| 48 | + // An environment identifier. |
| 49 | + environment: "staging", |
| 50 | + // Custom event transport that will be used to send things to Sentry |
| 51 | + transport: HappyTransport, |
| 52 | + // Method called for every captured event |
| 53 | + async beforeSend(event, hint) { |
| 54 | + // Because beforeSend and beforeBreadcrumb are async, user can fetch some data |
| 55 | + // return a promise, or whatever he wants |
| 56 | + // Our CustomError defined in errors.js has `someMethodAttachedToOurCustomError` |
| 57 | + // which can mimick something like a network request to grab more detailed error info or something. |
| 58 | + // hint is original exception that was triggered, so we check for our CustomError name |
| 59 | + if (hint.originalException.name === "CustomError") { |
| 60 | + const serverData = await hint.originalException.someMethodAttachedToOurCustomError(); |
| 61 | + event.extra = { |
| 62 | + ...event.extra, |
| 63 | + serverData |
| 64 | + }; |
| 65 | + } |
| 66 | + console.log(event); |
| 67 | + return event; |
| 68 | + }, |
| 69 | + // Method called for every captured breadcrumb |
| 70 | + beforeBreadcrumb(breadcrumb, hint) { |
| 71 | + // We ignore our own logger and rest of the buttons just for presentation purposes |
| 72 | + if (breadcrumb.message.startsWith("Sentry Logger")) return null; |
| 73 | + if (breadcrumb.category !== "ui.click" || hint.event.target.id !== "breadcrumb-hint") return null; |
| 74 | + |
| 75 | + // If we have a `ui.click` type of breadcrumb, eg. clicking on a button we defined in index.html |
| 76 | + // We will extract a `data-label` attribute from it and use it as a part of the message |
| 77 | + if (breadcrumb.category === "ui.click") { |
| 78 | + const label = hint.event.target.dataset.label; |
| 79 | + if (label) { |
| 80 | + breadcrumb.message = `User clicked on a button with label "${label}"`; |
| 81 | + } |
| 82 | + } |
| 83 | + console.log(breadcrumb); |
| 84 | + return breadcrumb; |
| 85 | + } |
| 86 | +}); |
| 87 | + |
| 88 | +// Testing code, irrelevant vvvvv |
| 89 | + |
| 90 | +document.addEventListener("DOMContentLoaded", () => { |
| 91 | + document.querySelector("#blacklist-url").addEventListener("click", () => { |
| 92 | + const script = document.createElement("script"); |
| 93 | + script.crossOrigin = "anonymous"; |
| 94 | + script.src = |
| 95 | + "https://rawgit.com/kamilogorek/cfbe9f92196c6c61053b28b2d42e2f5d/raw/3aef6ff5e2fd2ad4a84205cd71e2496a445ebe1d/external-lib.js"; |
| 96 | + document.body.appendChild(script); |
| 97 | + }); |
| 98 | + |
| 99 | + document.querySelector("#whitelist-url").addEventListener("click", () => { |
| 100 | + const script = document.createElement("script"); |
| 101 | + script.crossOrigin = "anonymous"; |
| 102 | + script.src = |
| 103 | + "https://rawgit.com/kamilogorek/cb67dafbd0e12b782bdcc1fbcaed2b87/raw/3aef6ff5e2fd2ad4a84205cd71e2496a445ebe1d/lib.js"; |
| 104 | + document.body.appendChild(script); |
| 105 | + }); |
| 106 | + |
| 107 | + document.querySelector("#ignore-message").addEventListener("click", () => { |
| 108 | + throw new Error("Exception that will be ignored because of this keyword => PickleRick_42 <="); |
| 109 | + }); |
| 110 | + |
| 111 | + document.querySelector("#ignore-type").addEventListener("click", () => { |
| 112 | + throw new RangeError("Exception that will be ignored because of it's type"); |
| 113 | + }); |
| 114 | + |
| 115 | + document.querySelector("#regular-exception").addEventListener("click", () => { |
| 116 | + throw new Error(`Regular exception no. ${Date.now()}`); |
| 117 | + }); |
| 118 | + |
| 119 | + document.querySelector("#capture-exception").addEventListener("click", () => { |
| 120 | + Sentry.captureException(new Error(`captureException call no. ${Date.now()}`)); |
| 121 | + }); |
| 122 | + |
| 123 | + document.querySelector("#capture-message").addEventListener("click", () => { |
| 124 | + Sentry.captureMessage(`captureMessage call no. ${Date.now()}`); |
| 125 | + }); |
| 126 | + |
| 127 | + document.querySelector("#duplicate-exception").addEventListener("click", () => { |
| 128 | + Sentry.captureException(new Error("duplicated exception")); |
| 129 | + }); |
| 130 | + |
| 131 | + document.querySelector("#duplicate-message").addEventListener("click", () => { |
| 132 | + Sentry.captureMessage("duplicate captureMessage"); |
| 133 | + }); |
| 134 | + |
| 135 | + document.querySelector("#integration-example").addEventListener("click", () => { |
| 136 | + Sentry.captureMessage("Happy Message"); |
| 137 | + }); |
| 138 | + |
| 139 | + document.querySelector("#exception-hint").addEventListener("click", () => { |
| 140 | + class CustomError extends Error { |
| 141 | + constructor(...args) { |
| 142 | + super(...args); |
| 143 | + this.name = "CustomError"; |
| 144 | + } |
| 145 | + someMethodAttachedToOurCustomError() { |
| 146 | + return new Promise(resolve => { |
| 147 | + resolve("some data, who knows what exactly"); |
| 148 | + }); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + throw new CustomError("Hey there"); |
| 153 | + }); |
| 154 | + |
| 155 | + document.querySelector("#breadcrumb-hint").addEventListener("click", () => {}); |
| 156 | +}); |
0 commit comments