การจัดการข้อผิดพลาดข้ามขอบเขต WASM
จัดการผลลัพธ์ ตัวเลือก และภาวะตื่นตระหนกของ Rust อย่างเรียบร้อยเมื่อข้ามเข้าสู่ JavaScript ด้วย wasm-bindgen
การจัดการข้อผิดพลาดข้ามขอบเขต WASM เป็นบทเรียน WebAssembly (WASM) for High Performance Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebAssembly (WASM) for High Performance Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebAssembly (WASM) for High Performance Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Errors Need a Bridge
Rust uses Result and Option for errors; JavaScript uses exceptions and null/undefined.
wasm-bindgen bridges these two worlds so failures surface naturally on each side.
Returning Result to JS
A Rust function returning Result<T, JsValue> becomes a JS function that either returns the value or throws.
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn parse(input: &str) -> Result<i32, JsValue> {
input.parse::<i32>()
.map_err(|e| JsValue::from_str(&e.to_string()))
}Catching It in JavaScript
On the JS side, an Err arrives as a thrown exception, so wrap calls in try/catch.
try {
const n = parse('abc');
} catch (e) {
console.error('Parse failed:', e);
}Mapping Custom Errors
Convert your own error types into a JsValue so JS gets a useful message. The ? operator plus map_err keeps this clean.
#[wasm_bindgen]
pub fn divide(a: i32, b: i32) -> Result<i32, JsValue> {
if b == 0 {
return Err(JsValue::from_str('division by zero'));
}
Ok(a / b)
}Returning Option
Rust's Option<T> maps to a value or undefined in JS, perfect for nullable results.
#[wasm_bindgen]
pub fn first_char(s: &str) -> Option<char> {
s.chars().next()
}What Happens on Panic
A Rust panic in WASM aborts the module and is hard to debug, by default you get an unhelpful 'unreachable' error.
Panics should be rare; prefer Result for expected failures.
Better Panic Messages
The console_error_panic_hook crate forwards panic messages to the browser console, making debugging far easier.
#[wasm_bindgen(start)]
pub fn main() {
console_error_panic_hook::set_once();
}Result vs Panic
Choose deliberately:
- Result, expected, recoverable errors (bad input, not found)
- Panic, programmer bugs and invariants that should never break
Never use panics for normal control flow across the boundary.
Using a Custom Error Type
For richer errors, define a struct exported to JS, so JS can read fields instead of just a string.
#[wasm_bindgen]
pub struct AppError { pub code: i32 }
#[wasm_bindgen]
impl AppError {
#[wasm_bindgen(getter)]
pub fn code(&self) -> i32 { self.code }
}Propagating with ?
Inside a function returning Result<_, JsValue>, the ? operator propagates errors cleanly when types convert.
#[wasm_bindgen]
pub fn run(input: &str) -> Result<i32, JsValue> {
let n: i32 = input.parse().map_err(|_| JsValue::from_str('bad'))?;
Ok(n * 2)
}Best Practices Summary
For robust error handling:
- Return
Result<T, JsValue>so errors become JS exceptions - Return
Optionfor nullable values - Install
console_error_panic_hookfor debuggable panics - Reserve panics for true bugs, use Result for expected failures
Quick Check
How does a Rust function returning Result<T, JsValue> behave when it returns Err and is called from JavaScript?
Recap
You now handle errors cleanly across the boundary:
- Result becomes a JS throw; Option becomes value-or-undefined
- Map custom errors into JsValue
- Use console_error_panic_hook to debug panics
- Prefer Result over panic for expected failures
Clear error semantics make Rust+WASM libraries pleasant and safe to consume from JS.
คำถามที่พบบ่อย
บทเรียน “การจัดการข้อผิดพลาดข้ามขอบเขต WASM” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจัดการข้อผิดพลาดข้ามขอบเขต WASM” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebAssembly (WASM) for High Performance Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebAssembly (WASM) for High Performance Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจัดการข้อผิดพลาดข้ามขอบเขต WASM”
จัดการผลลัพธ์ ตัวเลือก และภาวะตื่นตระหนกของ Rust อย่างเรียบร้อยเมื่อข้ามเข้าสู่ JavaScript ด้วย wasm-bindgen คุณปฏิบัติ WebAssembly (WASM) for High Performance Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebAssembly (WASM) for High Performance Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebAssembly (WASM) for High Performance Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การจัดการข้อผิดพลาดข้ามขอบเขต WASM” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน WebAssembly (WASM) for High Performance Apps นี้ได้ไหม
ได้ บทเรียน WebAssembly (WASM) for High Performance Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การตั้งค่า Rust สำหรับ WebAssembly
- การเขียนฟังก์ชัน Rust สำหรับ WASM
- การทำงานร่วมกับ JS อย่างมีประสิทธิภาพด้วย `wasm-bindgen`
- การจัดการข้อผิดพลาดข้ามขอบเขต WASM