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.
EDIT: added image of the error thrown by using postman to call the endpoint