hardyscc / nestjs-dynamoose

Dynamoose module for Nest
MIT License
139 stars 24 forks source link

throw built-in 404 when resource not found #859

Closed ianzone closed 1 year ago

ianzone commented 1 year ago

I'm submitting a...


[ ] Regression 
[ ] Bug report
[*] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

if a resource is not found, the get method only returns an empty object. it requires further check to throw the 404 error.

// users.service.ts
  async findOne(id: string) {
    const res = await this.users.get({id});
    if (!res) {
      throw new NotFoundException();
    }
    return res;
  }

Expected behavior

throw the built-in NotFoundException when the resource is not found. so the above code can be written as

// users.service.ts
  findOne(id: string) {
    return this.users.get({id});
  }

What is the motivation / use case for changing the behavior?

throw 404 when a resource is not found is a standard RESTful API behavior.

Environment


NestJS version: ^9.4.2
NestJS Dynamoose version: ^0.5.4
Dynamoose version: ^3.2.0


For Tooling issues:
- Node version: v18.16.0
- Platform:  Linux

hardyscc commented 1 year ago

@ianzone Since the model instance is directly return from dynamoose, it cannot be done easily.

hardyscc commented 1 year ago

add try catch example for 404 not found https://github.com/hardyscc/aws-nestjs-starter/commit/935b4d126e665d60580d680c36df19ae0069e52e