@42px/custom-errors 中文文档教程
Custom errors
用于方便错误处理的小型实用程序。
Installation
npm install @42px/custom-errors
Usage
import { createCustomError } from '@42px/custom-errors'
import { findRoom, doRoomStuff, showError } from 'rooms'
const RoomNotFound = createCustomError('RoomNotFound')
async function findAndProceed(id) {
const room = await findRoom(id)
if (!room) {
throw new RoomNotFound()
}
doRoomStuff(room)
}
findAndProceed(1)
.catch((err) => {
if(err.name === 'RoomNotFound') showError('Sorry, room not found')
})
Custom errors
Small util for convenient error handling.
Installation
npm install @42px/custom-errors
Usage
import { createCustomError } from '@42px/custom-errors'
import { findRoom, doRoomStuff, showError } from 'rooms'
const RoomNotFound = createCustomError('RoomNotFound')
async function findAndProceed(id) {
const room = await findRoom(id)
if (!room) {
throw new RoomNotFound()
}
doRoomStuff(room)
}
findAndProceed(1)
.catch((err) => {
if(err.name === 'RoomNotFound') showError('Sorry, room not found')
})