home / skills / cacr92 / wereply / error-handling-debugging

error-handling-debugging skill

/.trae/skills/error-handling-debugging

This skill helps you implement robust error handling and debugging across Rust, Tauri, and frontend by enforcing context and clear user prompts.

npx playbooks add skill cacr92/wereply --skill error-handling-debugging

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
1.3 KB
---
name: error-handling-debugging
description: 当用户要求错误处理、异常链路、错误提示或调试技巧时使用。
---

# Error Handling & Debugging Skill

## 适用范围
- Rust 错误链路与上下文
- Tauri 命令错误转换
- 前端用户提示与错误恢复

## 关键规则(Critical Rules)
- Rust 侧用 `anyhow::Context` 补充错误信息
- Tauri 命令统一返回 `ApiResponse<T>`
- 前端错误必须 `message.error` 提示用户

## Rust 错误链示例
```rust
use anyhow::{Context, Result};
use std::path::Path;

pub fn load_config(path: &Path) -> Result<String> {
    std::fs::read_to_string(path)
        .with_context(|| format!("读取配置失败: {}", path.display()))
}
```

## Tauri 命令错误转换
```rust
#[tauri::command]
#[specta::specta]
pub async fn get_settings() -> Result<ApiResponse<SettingsData>, String> {
    match get_settings_data_service().await {
        Ok(settings_data) => Ok(ApiResponse::success(settings_data)),
        Err(e) => Ok(ApiResponse::error(format!("获取设置数据失败: {}", e))),
    }
}
```

## 前端错误提示
```ts
import { message } from 'antd';

export function showError(text: string) {
  message.error(text);
}
```

## 检查清单
- [ ] Rust 错误带上下文
- [ ] Tauri 命令返回 `ApiResponse`
- [ ] 前端使用 `message.error` 提示

Overview

This skill provides practical rules and patterns for error handling and debugging across a Rust backend, Tauri command layer, and front-end UI. It focuses on enriching Rust errors with context, unifying Tauri command responses, and surfacing clear user-facing error messages. Use it to make failures easier to diagnose and to give users actionable feedback.

How this skill works

The skill inspects code paths where IO, async service calls, or FFI boundaries may fail and prescribes concrete transformations. In Rust it recommends using anyhow::Context to attach contextual messages. At the Tauri boundary it enforces returning ApiResponse<T> wrappers so errors are serializable. On the front end it standardizes showing errors via message.error so users see consistent prompts.

When to use it

  • When implementing Rust functions that perform IO or external calls
  • When writing Tauri commands that cross process boundaries
  • When designing front-end error prompts for user-facing failures
  • When debugging error chains to find root causes
  • When ensuring API responses are consistent and easy to parse

Best practices

  • Add contextual messages in Rust with anyhow::Context to explain what operation failed and why
  • Wrap Tauri command results in a unified ApiResponse<T> containing success or error payloads
  • Convert internal errors to safe, user-friendly strings at the Tauri boundary to avoid leaking internals
  • Always show front-end errors with a consistent UI function (e.g., message.error) so users see immediate feedback
  • Keep error messages actionable: mention the failed operation and a suggested next step or retry option

Example use cases

  • A Rust service failing to read a config file: use with_context to include the file path and operation
  • A Tauri command calling async services: return ApiResponse::error with a formatted message instead of panicking
  • A front-end submit flow that catches API errors and calls showError(text) to display a toast
  • Debugging a long error chain: start from the user-facing error, follow ApiResponse payload, and inspect Rust context annotations
  • Automated checklist before release: verify Rust context usage, Tauri response wrapping, and front-end error toasts are present

FAQ

Why add context in Rust errors?

Context clarifies where and why an operation failed, making stack traces and logs far easier to interpret during debugging.

Should internal error details be shown to users?

No. Convert internals into concise, user-friendly messages at the Tauri boundary and log full details server-side for diagnosis.