Replace Throw With Error Call

1.0.3Last update Dec 17, 2024
by@priyanshuthapliyal2005

Replace Throw Statements with Direct Function Calls

This codemod simplifies error handling and redirects in your TypeScript project by replacing throw error() and throw redirect() statements with direct calls to error() and redirect(). This change aligns with modern practices and results in cleaner, more readable code.

Transformations

This codemod performs the following transformations:

  • Error Handling: Transforms throw error(status, message) into error(status, message).
  • Redirects: Transforms throw redirect(status, url) into redirect(status, url).

Usage

To apply this codemod, run the workflow script on your TypeScript files. Ensure you have the necessary dependencies installed and your project is configured to use this codemod.

Example

Before

import { error } from '@sveltejs/kit';
throw error(500, 'Internal Server Error');
import { redirect } from '@sveltejs/kit';
throw redirect(301, '/new-location');
import { redirect } from '@sveltejs/kit';
// Redirect if the user is not authenticated
if (!user.isAuthenticated) {
throw redirect(303, '/login');
}

After

import { error } from '@sveltejs/kit';
error(500, 'Internal Server Error');
import { redirect } from '@sveltejs/kit';
redirect(301, '/new-location');
import { redirect } from '@sveltejs/kit';
// Redirect if the user is not authenticated
if (!user.isAuthenticated) {
redirect(303, '/login');
}

License

MIT License

Build custom codemods

Use AI-powered codemod studio and automate undifferentiated tasks for yourself, colleagues or the community

background illustrationGet Started Now