Function log shows rawCode: undefined when testing API endpoint

Site: accountability-bunnies.netlify.app

I’m testing an API endpoint with httpie.io

const fetch = require('node-fetch');

let url = 'http://accountability-bunnies.netlify.app/api/goals';

let options = {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: '{"id":123,"description":"this goal <> should not be added","completed":"no"}'
};

fetch(url, options)
  .then(res => res.json())
  .then(json => console.log(json))
  .catch(err => console.error('error:' + err));

The endpoint is

export const POST: APIRoute = async ({ request }) => {
    try {
        const data = await request.json();
        const newGoal = { ...data};
        const regex = /^(?!\s*$)[a-zA-Z0-9 ]+/;
        if (!regex.test(newGoal.description)) {
            return new Response('invalid description',{ status: 400 });
        }
        await db.insert(Goals).values(newGoal);
        return new Response(null,{ status: 201 });
    }
    catch (error) {
        console.error(error);
        return new Response('An error occurred.',{ status: 500 });
    }
};

I was expecting a response of status:400 invalid description,
but instead received 500 internal server error ‘an error occurred’.

This is the Netlify log for the function ASTRO SSR

Jun 6, 10:04:02 AM: a01c95ad ERROR DetailedLibsqlError [Astro DB Error]: SQL_QUERY_FAILED: Goals.id
Jun 6, 10:04:02 AM: a01c95ad ERROR at parseRemoteError (file:///var/task/node_modules/@astrojs/db/dist/runtime/db-client.js:163:10)
Jun 6, 10:04:02 AM: a01c95ad ERROR at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Jun 6, 10:04:02 AM: a01c95ad ERROR at async file:///var/task/node_modules/@astrojs/db/dist/runtime/db-client.js:49:17
Jun 6, 10:04:02 AM: a01c95ad ERROR at async safeFetch (file:///var/task/node_modules/@astrojs/db/dist/runtime/utils.js:9:5)
Jun 6, 10:04:02 AM: a01c95ad ERROR at async queries.map.sql.sql [as client] (file:///var/task/node_modules/@astrojs/db/dist/runtime/db-client.js:38:19)
Jun 6, 10:04:02 AM: a01c95ad ERROR at async Module.POST (file:///var/task/.netlify/functions-internal/ssr/ssr.mjs:2784:9)
Jun 6, 10:04:02 AM: a01c95ad ERROR at async renderEndpoint (file:///var/task/node_modules/astro/dist/runtime/server/endpoint.js:34:20)
Jun 6, 10:04:02 AM: a01c95ad ERROR at async callMiddleware (file:///var/task/node_modules/astro/dist/core/middleware/callMiddleware.js:21:10)
Jun 6, 10:04:02 AM: a01c95ad ERROR at async RenderContext.render (file:///var/task/node_modules/astro/dist/core/render-context.js:148:22)
Jun 6, 10:04:02 AM: a01c95ad ERROR at async App.render (file:///var/task/node_modules/astro/dist/core/app/index.js:233:18) {
Jun 6, 10:04:02 AM: a01c95ad ERROR code: ‘SQL_QUERY_FAILED’,
Jun 6, 10:04:02 AM: a01c95ad ERROR rawCode: undefined,
Jun 6, 10:04:02 AM: a01c95ad ERROR hint: ‘See the Astro DB guide for query and push instructions: Astro DB | Docs’,
Jun 6, 10:04:02 AM: a01c95ad ERROR [cause]: undefined
Jun 6, 10:04:02 AM: a01c95ad ERROR }
Jun 6, 10:04:02 AM: a01c95ad Duration: 153.98 ms Memory Usage: 152 MB

Testing a POST with a description that passes the regex works just fine.

Any ideas on why the error received was 500 instead of 400.

Thanks

Anything with at least one character at the start of the string passes.

/^(?!\s*$)[a-zA-Z0-9 ]+/.test('')
// return false

/^(?!\s*$)[a-zA-Z0-9 ]+/.test('<>')
// returns false

/^(?!\s*$)[a-zA-Z0-9 ]+/.test('<>a')
// returns false

/^(?!\s*$)[a-zA-Z0-9 ]+/.test('a')
// returns true

/^(?!\s*$)[a-zA-Z0-9 ]+/.test('a<>')
// returns true

/^(?!\s*$)[a-zA-Z0-9 ]+/.test('a <> a')
// returns true

Thanks for checking with the .test(). It appears the regex wasn’t working as I expected after all. I modified the regex to /^(?! *$)[a-zA-Z0-9 ]+$/ and now it works.

How extensively did you test this?

// Make some test values
let testValues = [
  123,
  "anv",
  "a<>b c",
  "a <> bc",
  "a b <> c",
  "ab<>c",
  "a<>",
  "<b>",
  "<>c",
  undefined,
  null,
  true,
  false
] 

// Now test them
testValues.forEach(v => console.log(v, ' = ', /^(?! *$)[a-zA-Z0-9 ]+$/.test(v)))

Results:

123  =  true
anv  =  true
a<>b c  =  false
a <> bc  =  false
a b <> c  =  false
ab<>c  =  false
a<>  =  false
<b>  =  false
<>c  =  false
undefined  =  true
null  =  true
true  =  true
false  =  true