Avoid unnecessary try catch in Typescript
When using try-catch blocks in Typescript, keep the try block focused only on the code that can raise the expected exception.
Bad:
```typescript
async function doSomething() {
try {
// Large block of code with multiple potential error sources
await fetchData()
Install this rule for wispbit
Add this rule to wispbit and it will run when you open a pull request
Install this rule for Coderabbit
Copy the configuration below and add it to your repository as .coderabbit.yml
in your project root.
reviews:
path_instructions:
- path: "*.ts"
instructions: |
When using try-catch blocks in Typescript, keep the try block focused only on the code that can raise the expected exception.
Bad:
```typescript
async function doSomething() {
try {
// Large block of code with multiple potential error sources
await fetchData()
await processData()
await saveResults()
} catch (error) {
console.error(`Error: ${(error as Error).message}`)
process.exit(1)
}
}
```
Good:
```typescript
async function doSomething() {
// Let errors propagate with their full stack trace
// or handle specific errors at appropriate points
await fetchData()
await processData()
await saveResults()
}
// If you need top-level error handling:
async function main() {
try {
await doSomething()
} catch (error) {
console.error("Unexpected error:", error)
process.exit(1)
}
}
```
Install this rule for Greptile
Greptile rules can be added through the web interface. Please see this documentation for details on how to add custom rules and context.
When using try-catch blocks in Typescript, keep the try block focused only on the code that can raise the expected exception.
Bad:
```typescript
async function doSomething() {
try {
// Large block of code with multiple potential error sources
await fetchData()
await processData()
await saveResults()
} catch (error) {
console.error(`Error: ${(error as Error).message}`)
process.exit(1)
}
}
```
Good:
```typescript
async function doSomething() {
// Let errors propagate with their full stack trace
// or handle specific errors at appropriate points
await fetchData()
await processData()
await saveResults()
}
// If you need top-level error handling:
async function main() {
try {
await doSomething()
} catch (error) {
console.error("Unexpected error:", error)
process.exit(1)
}
}
```
File Path Patterns:
Install this rule for GitHub Copilot
Copilot instructions can be added through the interface. See the documentation for details on how to create coding guidelines.
When using try-catch blocks in Typescript, keep the try block focused only on the code that can raise the expected exception.
Bad:
```typescript
async function doSomething() {
try {
// Large block of code with multiple potential error sources
await fetchData()
await processData()
await saveResults()
} catch (error) {
console.error(`Error: ${(error as Error).message}`)
process.exit(1)
}
}
```
Good:
```typescript
async function doSomething() {
// Let errors propagate with their full stack trace
// or handle specific errors at appropriate points
await fetchData()
await processData()
await saveResults()
}
// If you need top-level error handling:
async function main() {
try {
await doSomething()
} catch (error) {
console.error("Unexpected error:", error)
process.exit(1)
}
}
```
File Path Patterns:
Install this rule for Graphite Diamond
Diamond custom rules can be added through the interface. See the documentation for details on how to create custom rules.
When using try-catch blocks in Typescript, keep the try block focused only on the code that can raise the expected exception.
Bad:
```typescript
async function doSomething() {
try {
// Large block of code with multiple potential error sources
await fetchData()
await processData()
await saveResults()
} catch (error) {
console.error(`Error: ${(error as Error).message}`)
process.exit(1)
}
}
```
Good:
```typescript
async function doSomething() {
// Let errors propagate with their full stack trace
// or handle specific errors at appropriate points
await fetchData()
await processData()
await saveResults()
}
// If you need top-level error handling:
async function main() {
try {
await doSomething()
} catch (error) {
console.error("Unexpected error:", error)
process.exit(1)
}
}
```
File Path Patterns:
Use with Cline
Copy the rule below and ask Cline to review your code using this rule
When using try-catch blocks in Typescript, keep the try block focused only on the code that can raise the expected exception.
Bad:
```typescript
async function doSomething() {
try {
// Large block of code with multiple potential error sources
await fetchData()
await processData()
await saveResults()
} catch (error) {
console.error(`Error: ${(error as Error).message}`)
process.exit(1)
}
}
```
Good:
```typescript
async function doSomething() {
// Let errors propagate with their full stack trace
// or handle specific errors at appropriate points
await fetchData()
await processData()
await saveResults()
}
// If you need top-level error handling:
async function main() {
try {
await doSomething()
} catch (error) {
console.error("Unexpected error:", error)
process.exit(1)
}
}
```
Use with OpenAI Codex
Copy the rule below and ask OpenAI Codex to review your code using this rule
When using try-catch blocks in Typescript, keep the try block focused only on the code that can raise the expected exception.
Bad:
```typescript
async function doSomething() {
try {
// Large block of code with multiple potential error sources
await fetchData()
await processData()
await saveResults()
} catch (error) {
console.error(`Error: ${(error as Error).message}`)
process.exit(1)
}
}
```
Good:
```typescript
async function doSomething() {
// Let errors propagate with their full stack trace
// or handle specific errors at appropriate points
await fetchData()
await processData()
await saveResults()
}
// If you need top-level error handling:
async function main() {
try {
await doSomething()
} catch (error) {
console.error("Unexpected error:", error)
process.exit(1)
}
}
```
Use with Cursor
Copy the rule below and ask Cursor to review your code using this rule
When using try-catch blocks in Typescript, keep the try block focused only on the code that can raise the expected exception.
Bad:
```typescript
async function doSomething() {
try {
// Large block of code with multiple potential error sources
await fetchData()
await processData()
await saveResults()
} catch (error) {
console.error(`Error: ${(error as Error).message}`)
process.exit(1)
}
}
```
Good:
```typescript
async function doSomething() {
// Let errors propagate with their full stack trace
// or handle specific errors at appropriate points
await fetchData()
await processData()
await saveResults()
}
// If you need top-level error handling:
async function main() {
try {
await doSomething()
} catch (error) {
console.error("Unexpected error:", error)
process.exit(1)
}
}
```
Use with Claude Code
Copy the rule below and ask Claude Code to review your code using this rule
When using try-catch blocks in Typescript, keep the try block focused only on the code that can raise the expected exception.
Bad:
```typescript
async function doSomething() {
try {
// Large block of code with multiple potential error sources
await fetchData()
await processData()
await saveResults()
} catch (error) {
console.error(`Error: ${(error as Error).message}`)
process.exit(1)
}
}
```
Good:
```typescript
async function doSomething() {
// Let errors propagate with their full stack trace
// or handle specific errors at appropriate points
await fetchData()
await processData()
await saveResults()
}
// If you need top-level error handling:
async function main() {
try {
await doSomething()
} catch (error) {
console.error("Unexpected error:", error)
process.exit(1)
}
}
```
Install this rule for Windsurf
To set up rules for Windsurf Reviews, please see this documentation
When using try-catch blocks in Typescript, keep the try block focused only on the code that can raise the expected exception.
Bad:
```typescript
async function doSomething() {
try {
// Large block of code with multiple potential error sources
await fetchData()
await processData()
await saveResults()
} catch (error) {
console.error(`Error: ${(error as Error).message}`)
process.exit(1)
}
}
```
Good:
```typescript
async function doSomething() {
// Let errors propagate with their full stack trace
// or handle specific errors at appropriate points
await fetchData()
await processData()
await saveResults()
}
// If you need top-level error handling:
async function main() {
try {
await doSomething()
} catch (error) {
console.error("Unexpected error:", error)
process.exit(1)
}
}
```