skip to Main Content

I have a nestjs application in which I created a pipe to check if value is a valid URL.
If the URL is invalid, throw error. This is used in a controller to save an item into the database.

I just realized the pipe would just throw 500 with Internal Server Error, I had to check the server log to see if it’s actually from the pipe.

I wonder if there’s a way that we can have http response directly with the message?

The pipe code I have is…

import { BadRequestException, Injectable, PipeTransform } from '@nestjs/common';

@Injectable()
export class ValidUrlPipe implements PipeTransform {
  transform(website: string) {
    if (website === '') return website;

    const validProtocols = ['http:', 'https:'];

    const { protocol } = new URL(website);
    if (validProtocols.includes(protocol)) {
      return website;
    }

    throw new BadRequestException(`invalid website value: ${website}`);
  }
}

the controller using the pipe looks like

  @Post()
  create(
    @Body('website', ValidUrlPipe) website: string,
    @Body() createTvDto: CreateTvDto,
    @CurrentUser() user: User,
  ) {
    return this.televisionsService.create(user._id, createTvDto);
  }

Thank you so much in advance for any suggestions/solutions.

enter image description here

EDIT: added image of the error thrown by using postman to call the endpoint

2

Answers


  1. Your issue is that the new URL() call is throwing an exception. You can wrap that in a try/catch block to transform it to the proper BadRequestException you are wanting to throw. Otherwise, I’d probably just use a simple regex /https?:///.test(website) to validate that the passed website is indeed an HTTP or HTTPS website.

    Login or Signup to reply.
  2. this line const { protocol } = new URL(website); will throw an error if the website parameter is not a valid URL. you could put validation to check if the parameter is a valid URL. it’s better to have a try/ catch block to catch errors and return proper exceptions.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search