@@ -79,41 +79,48 @@ async function injectBanner(response, bannerMessage) {
7979 return new Response ( text , response ) ;
8080}
8181
82+ // New function to handle /report-error POST for Discord webhook
83+ async function handleReportError ( request , env ) {
84+ try {
85+ const { fullName, errorCode, siteName, redirectUrl } = await request . json ( ) ;
86+ if ( ! fullName || ! errorCode || ! siteName || ! redirectUrl ) {
87+ return new Response ( JSON . stringify ( { ok : false , error : 'Missing required fields' } ) , { status : 400 , headers : { 'Content-Type' : 'application/json' } } ) ;
88+ }
89+ const embed = {
90+ title : '🆘 Erreur Signalée' ,
91+ color : 0xef4444 , // Red color
92+ fields : [
93+ { name : 'Nom / Prénom' , value : fullName , inline : true } ,
94+ { name : 'Code d\'Erreur' , value : errorCode , inline : true } ,
95+ { name : 'Site' , value : siteName , inline : true } ,
96+ { name : 'URL' , value : redirectUrl , inline : false }
97+ ] ,
98+ timestamp : new Date ( ) . toISOString ( )
99+ } ;
100+ const webhookResponse = await fetch ( env . DISCORD_WEBHOOK_URL , {
101+ method : 'POST' ,
102+ headers : { 'Content-Type' : 'application/json' } ,
103+ body : JSON . stringify ( { embeds : [ embed ] } )
104+ } ) ;
105+ if ( ! webhookResponse . ok ) {
106+ throw new Error ( `Webhook failed: ${ webhookResponse . status } ` ) ;
107+ }
108+ return new Response ( JSON . stringify ( { ok : true } ) , { headers : { 'Content-Type' : 'application/json' } } ) ;
109+ } catch ( error ) {
110+ console . error ( 'Report error handling failed:' , error ) ;
111+ return new Response ( JSON . stringify ( { ok : false , error : 'Internal error' } ) , { status : 500 , headers : { 'Content-Type' : 'application/json' } } ) ;
112+ }
113+ }
114+
82115export default {
83116 async fetch ( request , env , ctx ) {
84117 const host = request . headers . get ( 'host' ) ;
85118 const url = new URL ( request . url ) ;
86119
87- // New: Handle /report-error POST for Discord webhook
88- if ( url . pathname === '/report-error' && request . method === 'POST' ) {
89- try {
90- const { fullName, errorCode, siteName, redirectUrl } = await request . json ( ) ;
91- if ( ! fullName || ! errorCode || ! siteName || ! redirectUrl ) {
92- return new Response ( JSON . stringify ( { ok : false , error : 'Missing required fields' } ) , { status : 400 , headers : { 'Content-Type' : 'application/json' } } ) ;
93- }
94- const embed = {
95- title : '🆘 Erreur Signalée' ,
96- color : 0xef4444 , // Red color
97- fields : [
98- { name : 'Nom / Prénom' , value : fullName , inline : true } ,
99- { name : 'Code d\'Erreur' , value : errorCode , inline : true } ,
100- { name : 'Site' , value : siteName , inline : true } ,
101- { name : 'URL' , value : redirectUrl , inline : false }
102- ] ,
103- timestamp : new Date ( ) . toISOString ( )
104- } ;
105- const webhookResponse = await fetch ( env . DISCORD_WEBHOOK_URL , {
106- method : 'POST' ,
107- headers : { 'Content-Type' : 'application/json' } ,
108- body : JSON . stringify ( { embeds : [ embed ] } )
109- } ) ;
110- if ( ! webhookResponse . ok ) {
111- throw new Error ( `Webhook failed: ${ webhookResponse . status } ` ) ;
112- }
113- return new Response ( JSON . stringify ( { ok : true } ) , { headers : { 'Content-Type' : 'application/json' } } ) ;
114- } catch ( error ) {
115- console . error ( 'Report error handling failed:' , error ) ;
116- return new Response ( JSON . stringify ( { ok : false , error : 'Internal error' } ) , { status : 500 , headers : { 'Content-Type' : 'application/json' } } ) ;
120+ // Handle /report-error POST for Discord webhook using the new function
121+ if ( env . ENABLE_REPORTING_ERRORS === true || env . ENABLE_REPORTING_ERRORS === 'true' ) {
122+ if ( url . pathname === '/report-error' && request . method === 'POST' ) {
123+ return await handleReportError ( request , env ) ;
117124 }
118125 }
119126
0 commit comments