Skip to content

Add sentry #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions supabase/functions/sentry/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Follow this setup guide to integrate the Deno language server with your editor:
// https://deno.land/manual/getting_started/setup_your_environment
// This enables autocomplete, go to definition, etc.

console.log('Hello from the Sentry Functions Challenge!')

import * as Sentry from 'https://deno.land/x/[email protected]/index.mjs'

Sentry.init({
dsn: Deno.env.get('SENTRY_DSN'),
integrations: [],
debug: false,
// Performance Monitoring
tracesSampleRate: 1.0,
})

// Set region and execution_id as custom tags
Sentry.setTag('region', Deno.env.get('SB_REGION'))
Sentry.setTag('execution_id', Deno.env.get('SB_EXECUTION_ID'))

console.log('Hello from Functions!')

Deno.serve(async (req) => {
try {
if (req.method === 'GET') {
return new Response(
'What is Supabase not? POST {answer, twitter} to this URL! Need a hint? 👉 https://github.com/supabase/supabase/blob/master/examples/edge-functions/supabase/functions/sentry/index.ts'
)
}
let answer: string
let twitter: string
try {
const params = await req.json()
if (!params?.answer || !params?.twitter) throw new Error('no answer')
answer = params.answer
twitter = params.twitter
} catch (_) {
return new Response(
'You need to send a JSON body with {answer, twitter}!'
)
}
if (answer.toLowerCase() !== 'postgres') {
return new Response(
`You are correct! But that means you've failed the challenge. Please try again!`
)
} else {
throw { twitter }
}
} catch (e) {
Sentry.captureException(e)
return new Response(
JSON.stringify({
msg: `Congrats, you are wrong https://itsjustpostgres.com/ 🎉 @${e.twitter} has been added to the draw!`,
}),
{
status: 500,
headers: { 'Content-Type': 'application/json' },
}
)
}
})

/* To invoke locally:

1. Run `supabase start` (see: https://supabase.com/docs/reference/cli/supabase-start)
2. Make an HTTP request:

curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/sentry' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0' \
--header 'Content-Type: application/json' \
--data '{"answer":"mysql", "twitter":"thorwebdev"}'

*/