Any way to view pages logs?

I'm trying to debug MailChannels not working and can't get any sort of data to log at all. I tried returning response objects from the SvelteKit actions and then logging them through "Real-time Logs", but those always return OK even when the status is 500, and they also don't include any of the response data that I try to attach. Attached below is my latest request. (says OK even though it errors?)
No description
No description
20 Replies
x03
x03OP10mo ago
If needed, here's my code:
// Function to send an email
export async function sendEmail(email: string, subject: string, value: string) {
const response = await fetch("https://api.mailchannels.net/tx/v1/send", {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
// ...
}),
})

return response
}

// Function to email the code
export async function emailCode(codes: KVNamespace, email: string) {
const code = Math.floor(100000 + Math.random() * 900000)

// Set the key in KV
await codes.put(email, code.toString(), {
expirationTtl: EXPIRATION_TIME / 1000,
})

if (dev) {
console.log(`CODE: ${code}`)
return new Response(null, { status: 200 })
}

const result = await sendEmail(
email,
"Verification Code",
code.toString()
)

return result
}

// Actions
export const actions = {
email: async (event) => {
const form = await superValidate(event, zod(emailSchema))

if (!form.valid) {
return fail(400, {
form,
})
}

if (!event.platform?.env?.codes) {
return fail(500, {
form,
})
}

const response = await emailCode(
event.platform?.env?.codes,
form.data.email
)

// This is where the error gets "cought", but I have no way of logging what it is
if (!response.ok) {
return fail(response.status, {
form,
})
}

// Set cookies to persist state
// ...

return { form }
},
}
// Function to send an email
export async function sendEmail(email: string, subject: string, value: string) {
const response = await fetch("https://api.mailchannels.net/tx/v1/send", {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
// ...
}),
})

return response
}

// Function to email the code
export async function emailCode(codes: KVNamespace, email: string) {
const code = Math.floor(100000 + Math.random() * 900000)

// Set the key in KV
await codes.put(email, code.toString(), {
expirationTtl: EXPIRATION_TIME / 1000,
})

if (dev) {
console.log(`CODE: ${code}`)
return new Response(null, { status: 200 })
}

const result = await sendEmail(
email,
"Verification Code",
code.toString()
)

return result
}

// Actions
export const actions = {
email: async (event) => {
const form = await superValidate(event, zod(emailSchema))

if (!form.valid) {
return fail(400, {
form,
})
}

if (!event.platform?.env?.codes) {
return fail(500, {
form,
})
}

const response = await emailCode(
event.platform?.env?.codes,
form.data.email
)

// This is where the error gets "cought", but I have no way of logging what it is
if (!response.ok) {
return fail(response.status, {
form,
})
}

// Set cookies to persist state
// ...

return { form }
},
}
There's more to the code but I cut out the irrelevant stuff. Maybe returning something from the actions could work?
Cyb3r-Jak3
Cyb3r-Jak310mo ago
The Ok status just means that a response was returned. It doesn't need to a be a 200 response
x03
x03OP10mo ago
Oh, makes sense. How can I somehow log / return data about why the email send function failed?
Cyb3r-Jak3
Cyb3r-Jak310mo ago
I believe so, just it needs to be triggered by the email handler and not the on fetch handler
x03
x03OP10mo ago
If doing something like console.log, can you view the output anywhere on cloudflare? If not, how else do I return info on why it failed? I tried returning data like this but it did not work:
No description
x03
x03OP10mo ago
I need some way of finding out what the response of emailCode is, but its not an actual endpoint so logs dont catch it
Cyb3r-Jak3
Cyb3r-Jak310mo ago
You should be able to do it in the workers logs, but unless you have a logpush enabled for the worker it won't be saved.
x03
x03OP10mo ago
this is a pages app I was not able to find any sort of pages logs through the CF dashboard besides the request logger
Cyb3r-Jak3
Cyb3r-Jak310mo ago
That is the request logger where you can see it. If you are using console.log then those entries should appear in the request logger.
x03
x03OP10mo ago
Weird, the logged did not catch any console.log's before. Let me try re-adding them to test this again.
Chaika
Chaika10mo ago
There's some reasons you may not want to use mailchannels: https://discord.com/channels/595317990191398933/779390076219686943/1152689451374477483 But if you have no other option, I'd ask if you added the mailchannels lockdown txt to allow it to work?
x03
x03OP10mo ago
Oh wow, you're right. idk how I didnt notice this before
No description
x03
x03OP10mo ago
Thanks for this I did add lockdown let me read the attached message
Chaika
Chaika10mo ago
what lockdown txt record did you add? With Pages it should be like _mailchannels TXT v=mc1 cfid=xxxxx.pages.dev where xxxx is your pages project.dev, like _mailchannels TXT v=mc1 cfid=quick-test.pages.dev
x03
x03OP10mo ago
No description
x03
x03OP10mo ago
yeah I read somewhere to add xx.pages.dev, still did not work I've tried a lot of thing to get mailchannels to work and it just does not want to work. I might honestly just use amazon SES. The security issues you linked to are also troublesome
Chaika
Chaika10mo ago
Why do you have mutiple cfids? I don't see that mentioned as being supported in any of their examples/docs
x03
x03OP10mo ago
I've tried just doing one cfid with my .pages.dev, still had the same result
Chaika
Chaika10mo ago
it's not a bad idea to just use something else, but otherwise I'd log the response text and see what the api is responding with
x03
x03OP10mo ago
Alright, I'll try to fix mailchannels just to track down the issue and then probably switch after that, appreciate all the help 🙏
Want results from more Discord servers?
Add your server