kleydon / prisma-session-store

Express session store for Prisma
MIT License
116 stars 18 forks source link

No session found error keeps crashing app. How to identify cause or return request error #82

Closed efhan-the-unorthodox closed 2 years ago

efhan-the-unorthodox commented 2 years ago

Background

I know that the community for this library is kinda small but also this is the library that I've seen that fits perfectly in my use case together with Prisma ORM. So basically I use this library alongside prisma as my session-store for storing sessions in my express app when users login. In my development environment, I don't seem to face any problem when logging in and usually the session is able to persist for the amount of time I gave (which was about 30 days). However, after putting into production, my testers on the client side have complained a few times already of losing connection to the server after doing typical CRUD functions. I looked into my logs and so far I've only managed to find this from the log which says as follows:

PrismaClientKnownRequestError: 
Invalid `this.prisma[this.sessionModelName].update()` invocation in
/app/node_modules/@quixo3/prisma-session-store/dist/lib/prisma-session-store.js:483:81

  480     id: this.dbRecordIdIsSessionId ? sid : this.dbRecordIdFunction(sid),
  481 };
  482 if (!(existingSession !== null)) return [3 /*break*/, 4];
→ 483 return [4 /*yield*/, this.prisma[this.sessionModelName].update(
  An operation failed because it depends on one or more records that were required but not found. Record to update not found.
    at Object.request (/app/node_modules/@prisma/client/runtime/index.js:45629:15)
    at async PrismaClient._request (/app/node_modules/@prisma/client/runtime/index.js:46456:18) {
  code: 'P2025',
  clientVersion: '3.13.0',
  meta: { cause: 'Record to update not found.' }
}

So apparently from the error log, this is a Prisma Error of P2025 where it says "An operation failed because it depends on one or more records that were required but not found." In which apparently it couldn't find the record to update. However, this is the part where I don't understand because nowhere am I actually calling to check the session and whatever it is, is there a way to get the app to return an error rather than just crashing the entire server?

kleydon commented 2 years ago

Hi @efhan-the-unorthodox - thanks for posting this issue.

I've just pushed a new release (3.1.4) via PR #84 that prevents the crash, while relaying the error, via express session store's convention for doing so.

store.set() takes a callback, which in turn takes an error argument, i.e. store.set(sid, session, (error)=>{...}); see here for the approach.

Going to close this for now... If problems persist - or if you learn more about the specific circumstances under which P2025 occurs - I'd love to know about it.

Cheers.

efhan-the-unorthodox commented 1 year ago

Hi @efhan-the-unorthodox - thanks for posting this issue.

I've just pushed a new release (3.1.4) via PR #84 that prevents the crash, while relaying the error, via express session store's convention for doing so.

store.set() takes a callback, which in turn takes an error argument, i.e. store.set(sid, session, (error)=>{...}); see here for the approach.

Going to close this for now... If problems persist - or if you learn more about the specific circumstances under which P2025 occurs - I'd love to know about it.

Cheers.

I know this is late. But thank you for the help. Much appreciated.

kleydon commented 1 year ago

👍