|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import * as Sentry from "@sentry/nextjs"; |
| 4 | +import Head from "next/head"; |
| 5 | +import { useEffect, useState } from "react"; |
| 6 | + |
| 7 | +class SentryExampleFrontendError extends Error { |
| 8 | + constructor(message: string | undefined) { |
| 9 | + super(message); |
| 10 | + this.name = "SentryExampleFrontendError"; |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +export default function Page() { |
| 15 | + const [hasSentError, setHasSentError] = useState(false); |
| 16 | + const [isConnected, setIsConnected] = useState(true); |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + Sentry.logger.info("Sentry example page loaded"); |
| 20 | + async function checkConnectivity() { |
| 21 | + const result = await Sentry.diagnoseSdkConnectivity(); |
| 22 | + setIsConnected(result !== "sentry-unreachable"); |
| 23 | + } |
| 24 | + checkConnectivity(); |
| 25 | + }, []); |
| 26 | + |
| 27 | + return ( |
| 28 | + <div> |
| 29 | + <Head> |
| 30 | + <title>sentry-example-page</title> |
| 31 | + <meta name="description" content="Test Sentry for your Next.js app!" /> |
| 32 | + </Head> |
| 33 | + |
| 34 | + <main> |
| 35 | + <div className="flex-spacer" /> |
| 36 | + <svg |
| 37 | + height="40" |
| 38 | + width="40" |
| 39 | + fill="none" |
| 40 | + xmlns="http://www.w3.org/2000/svg" |
| 41 | + role="img" |
| 42 | + aria-label="Sentry logo" |
| 43 | + > |
| 44 | + <path |
| 45 | + d="M21.85 2.995a3.698 3.698 0 0 1 1.353 1.354l16.303 28.278a3.703 3.703 0 0 1-1.354 5.053 3.694 3.694 0 0 1-1.848.496h-3.828a31.149 31.149 0 0 0 0-3.09h3.815a.61.61 0 0 0 .537-.917L20.523 5.893a.61.61 0 0 0-1.057 0l-3.739 6.494a28.948 28.948 0 0 1 9.63 10.453 28.988 28.988 0 0 1 3.499 13.78v1.542h-9.852v-1.544a19.106 19.106 0 0 0-2.182-8.85 19.08 19.08 0 0 0-6.032-6.829l-1.85 3.208a15.377 15.377 0 0 1 6.382 12.484v1.542H3.696A3.694 3.694 0 0 1 0 34.473c0-.648.17-1.286.494-1.849l2.33-4.074a8.562 8.562 0 0 1 2.689 1.536L3.158 34.17a.611.611 0 0 0 .538.917h8.448a12.481 12.481 0 0 0-6.037-9.09l-1.344-.772 4.908-8.545 1.344.77a22.16 22.16 0 0 1 7.705 7.444 22.193 22.193 0 0 1 3.316 10.193h3.699a25.892 25.892 0 0 0-3.811-12.033 25.856 25.856 0 0 0-9.046-8.796l-1.344-.772 5.269-9.136a3.698 3.698 0 0 1 3.2-1.849c.648 0 1.285.17 1.847.495Z" |
| 46 | + fill="currentcolor" |
| 47 | + /> |
| 48 | + </svg> |
| 49 | + <h1>sentry-example-page</h1> |
| 50 | + |
| 51 | + <p className="description"> |
| 52 | + Click the button below, and view the sample error on the Sentry{" "} |
| 53 | + <a |
| 54 | + target="_blank" |
| 55 | + rel="noopener" |
| 56 | + href="https://harshorg.sentry.io/issues/?project=4511263762284624" |
| 57 | + > |
| 58 | + Issues Page |
| 59 | + </a> |
| 60 | + . For more details about setting up Sentry,{" "} |
| 61 | + <a |
| 62 | + target="_blank" |
| 63 | + rel="noopener" |
| 64 | + href="https://docs.sentry.io/platforms/javascript/guides/nextjs/" |
| 65 | + > |
| 66 | + read our docs |
| 67 | + </a> |
| 68 | + . |
| 69 | + </p> |
| 70 | + |
| 71 | + <button |
| 72 | + type="button" |
| 73 | + onClick={async () => { |
| 74 | + Sentry.logger.info("User clicked the button, throwing a sample error"); |
| 75 | + await Sentry.startSpan( |
| 76 | + { |
| 77 | + name: "Example Frontend/Backend Span", |
| 78 | + op: "test", |
| 79 | + }, |
| 80 | + async () => { |
| 81 | + const res = await fetch("/api/sentry-example-api"); |
| 82 | + if (!res.ok) { |
| 83 | + setHasSentError(true); |
| 84 | + } |
| 85 | + }, |
| 86 | + ); |
| 87 | + throw new SentryExampleFrontendError( |
| 88 | + "This error is raised on the frontend of the example page.", |
| 89 | + ); |
| 90 | + }} |
| 91 | + disabled={!isConnected} |
| 92 | + > |
| 93 | + <span>Throw Sample Error</span> |
| 94 | + </button> |
| 95 | + |
| 96 | + {hasSentError ? ( |
| 97 | + <p className="success">Error sent to Sentry.</p> |
| 98 | + ) : !isConnected ? ( |
| 99 | + <div className="connectivity-error"> |
| 100 | + <p> |
| 101 | + It looks like network requests to Sentry are being blocked, which |
| 102 | + will prevent errors from being captured. Try disabling your |
| 103 | + ad-blocker to complete the test. |
| 104 | + </p> |
| 105 | + </div> |
| 106 | + ) : ( |
| 107 | + <div className="success_placeholder" /> |
| 108 | + )} |
| 109 | + |
| 110 | + <div className="flex-spacer" /> |
| 111 | + </main> |
| 112 | + |
| 113 | + <style>{` |
| 114 | + main { |
| 115 | + display: flex; |
| 116 | + min-height: 100vh; |
| 117 | + flex-direction: column; |
| 118 | + justify-content: center; |
| 119 | + align-items: center; |
| 120 | + gap: 16px; |
| 121 | + padding: 16px; |
| 122 | + font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif; |
| 123 | + } |
| 124 | +
|
| 125 | + h1 { |
| 126 | + padding: 0px 4px; |
| 127 | + border-radius: 4px; |
| 128 | + background-color: rgba(24, 20, 35, 0.03); |
| 129 | + font-family: monospace; |
| 130 | + font-size: 20px; |
| 131 | + line-height: 1.2; |
| 132 | + } |
| 133 | +
|
| 134 | + p { |
| 135 | + margin: 0; |
| 136 | + font-size: 20px; |
| 137 | + } |
| 138 | +
|
| 139 | + a { |
| 140 | + color: #6341F0; |
| 141 | + text-decoration: underline; |
| 142 | + cursor: pointer; |
| 143 | +
|
| 144 | + @media (prefers-color-scheme: dark) { |
| 145 | + color: #B3A1FF; |
| 146 | + } |
| 147 | + } |
| 148 | +
|
| 149 | + button { |
| 150 | + border-radius: 8px; |
| 151 | + color: white; |
| 152 | + cursor: pointer; |
| 153 | + background-color: #553DB8; |
| 154 | + border: none; |
| 155 | + padding: 0; |
| 156 | + margin-top: 4px; |
| 157 | +
|
| 158 | + & > span { |
| 159 | + display: inline-block; |
| 160 | + padding: 12px 16px; |
| 161 | + border-radius: inherit; |
| 162 | + font-size: 20px; |
| 163 | + font-weight: bold; |
| 164 | + line-height: 1; |
| 165 | + background-color: #7553FF; |
| 166 | + border: 1px solid #553DB8; |
| 167 | + transform: translateY(-4px); |
| 168 | + } |
| 169 | +
|
| 170 | + &:hover > span { |
| 171 | + transform: translateY(-8px); |
| 172 | + } |
| 173 | +
|
| 174 | + &:active > span { |
| 175 | + transform: translateY(0); |
| 176 | + } |
| 177 | +
|
| 178 | + &:disabled { |
| 179 | + cursor: not-allowed; |
| 180 | + opacity: 0.6; |
| 181 | +
|
| 182 | + & > span { |
| 183 | + transform: translateY(0); |
| 184 | + border: none |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | +
|
| 189 | + .description { |
| 190 | + text-align: center; |
| 191 | + color: #6E6C75; |
| 192 | + max-width: 500px; |
| 193 | + line-height: 1.5; |
| 194 | + font-size: 20px; |
| 195 | +
|
| 196 | + @media (prefers-color-scheme: dark) { |
| 197 | + color: #A49FB5; |
| 198 | + } |
| 199 | + } |
| 200 | +
|
| 201 | + .flex-spacer { |
| 202 | + flex: 1; |
| 203 | + } |
| 204 | +
|
| 205 | + .success { |
| 206 | + padding: 12px 16px; |
| 207 | + border-radius: 8px; |
| 208 | + font-size: 20px; |
| 209 | + line-height: 1; |
| 210 | + background-color: #00F261; |
| 211 | + border: 1px solid #00BF4D; |
| 212 | + color: #181423; |
| 213 | + } |
| 214 | +
|
| 215 | + .success_placeholder { |
| 216 | + height: 46px; |
| 217 | + } |
| 218 | +
|
| 219 | + .connectivity-error { |
| 220 | + padding: 12px 16px; |
| 221 | + background-color: #E50045; |
| 222 | + border-radius: 8px; |
| 223 | + width: 500px; |
| 224 | + color: #FFFFFF; |
| 225 | + border: 1px solid #A80033; |
| 226 | + text-align: center; |
| 227 | + margin: 0; |
| 228 | + } |
| 229 | +
|
| 230 | + .connectivity-error a { |
| 231 | + color: #FFFFFF; |
| 232 | + text-decoration: underline; |
| 233 | + } |
| 234 | + `}</style> |
| 235 | + </div> |
| 236 | + ); |
| 237 | +} |
0 commit comments