ì¬ì©ì ì ì ì€ë¥ ì íì 구ííì¬ TypeScript ê°ë°ì í¥ììí€ìžì. ë ëª íí ëë²ê¹ 곌 ëì± íë ¥ì ìž ì í늬ìŒìŽì ì ìíŽ í¹ì ì€ë¥ë¥Œ ìì±, ë°ì ë° í¬ì°©íë ë°©ë²ì ìì볎ìžì.
Mastering TypeScript Error Messages: Crafting Custom Error Types for Robust Applications
ìíížìšìŽ ê°ë°ì ìëì ìž ìžê³ìì ì€ë¥ë¥Œ ì ìì ìŒë¡ ì²ëЬíë ê²ì íë ¥ ìê³ ì ì§ êŽëЬ ê°ë¥í ì í늬ìŒìŽì ì 구ì¶íë ë° ë§€ì° ì€ìí©ëë€. TypeScriptë ê°ë ¥í íìŽí ìì€í ì íµíŽ ì»ŽíìŒ ìê°ì ì ì¬ì ìž ë¬žì 륌 ë§ìŽ ì¡ì ì ìë ê°ë ¥í êž°ë°ì ì ê³µí©ëë€. ê·žë¬ë ë°íì ì€ë¥ë 몚ë ì í늬ìŒìŽì ìì íŒí ì ìë ë¶ë¶ì ëë€. TypeScriptì Ʞ볞 ì ê³µ ì€ë¥ ì²ëЬ ë©ì»€ëìŠì ê°ë ¥íì§ë§ ë 구첎ì ìž ì»ší ì€íž ìžì ì€ë¥ êŽëŠ¬ê° íìí 겜ì°ê° ììµëë€. ì¬êž°ì ì¬ì©ì ì ì ì€ë¥ ì íì 구íì ì ìžê³ ê°ë°ììê² ììŽìë ì ë ëêµ¬ê° ë©ëë€.
ìŽ í¬êŽì ìž ê°ìŽëë TypeScriptìì ì¬ì©ì ì ì ì€ë¥ ì íì ìì±, íì© ë° êŽëЬíë ë³µì¡ì±ì ììží ìŽíŽëŽ ëë€. ì°ëЬë ìŽì , ì€ì©ì ìž êµ¬í ì ëµì ìŽíŽë³Žê³ ì§ëЬì ìì¹ë í ê·ëªšì êŽê³ììŽ ëªšë ê·ëªšì íë¡ì ížì ì ì©í ì ìë ì€í ê°ë¥í íµì°°ë ¥ì ì ê³µí ê²ì ëë€.
Why Custom Error Types Matter in Global Development
ì°ëŠ¬ê° 'ë°©ë²'ì ë°ìŽë€êž° ì ì 'ìŽì '륌 í늜í©ìë€. í¹í êµì íìì ìŒíê±°ë êžë¡ë² ì¬ì©ì êž°ë°ì ìë¹ì€íë ê°ë°ìë ì ì¬ì©ì ì ì ì€ë¥ ì íì ìê°ì í¬ìíŽìŒ í ê¹ì? ê·ž ìŽì ë ì¬ë¬ ê°ì§ê° ììµëë€.
- í¥ìë ëª íì± ë° ê°ë ì±: ìŒë°ì ìž ì€ë¥ ë©ìì§ë ìŽíŽíêž° ìŽë µê³ ëììŽ ëì§ ìì ì ììµëë€. ì¬ì©ì ì ì ì€ë¥ ì íì ì¬ì©í멎 묞ì ì í¹ì±ì ëª ííê² ëíëŽë í¹ì ì€ëª ë©ìì§ë¥Œ ì ê³µíì¬ ëë²ê¹ ìë륌 í¬ê² ëìŒ ì ììµëë€. í¹í ë€ë¥ž ìê°ëì ìë ê°ë°ìê° ì²ììŒë¡ 묞ì 륌 겪ì ì ììµëë€.
- í¥ìë ëë²ê¹ íšìšì±: ì€ë¥ê° ë°ìíì ë ì íí 묎ììŽ ì못ëìëì§ ìë ê²ìŽ ì€ìí©ëë€. ì¬ì©ì ì ì ì€ë¥ ì íì ì¬ì©í멎 ì€ë¥ë¥Œ ë¶ë¥íì¬ ê°ë°ìê° ì€ë¥ì ììžê³Œ 컚í ì€ížë¥Œ ì ìíê² ì íí íì í ì ììµëë€. ìŽë ì§ì ì ìž íì ìŽ ì íë ì ìë ë¶ì° íì ë§€ì° ì€ìí©ëë€.
- ìžë¶íë ì€ë¥ ì²ëЬ: 몚ë ì€ë¥ê° ëìŒíê² ìì±ëë ê²ì ìëëë€. ìŒë¶ë 복구í ì ìì§ë§ ë€ë¥ž ìŒë¶ë ì¹ëª ì ìž ì€ë¥ë¥Œ ëíë ëë€. ì¬ì©ì ì ì ì€ë¥ ì íì ì¬ì©í멎 ë€ìí ì€ë¥ ë²ì£Œì ëí í¹ì catch ëžë¡ì 구ííì¬ ë³Žë€ íê²í ëê³ ì§ë¥ì ìž ì€ë¥ 복구 ì ëµì ì¬ì©í ì ììµëë€. ì륌 ë€ìŽ ë€ížìí¬ ì€ë¥ë ì¬ìëí ì ìì§ë§ ìžìŠ ì€ë¥ìë ë€ë¥ž ì¬ì©ì íëŠìŽ íìí©ëë€.
- ëë©ìžë³ ì 볎: ì í늬ìŒìŽì
ì í¹ì ëë©ìž(ì: ì ì ìê±°ë, êžìµ, ìë£) ëŽìì ìëí ê°ë¥ì±ìŽ ëìµëë€. ì¬ì©ì ì ì ì€ë¥ ì íì ëë©ìžë³ ë°ìŽí°ë¥Œ 캡ìííì¬ ë íë¶í 컚í
ì€ížë¥Œ ì ê³µí ì ììµëë€. ì륌 ë€ìŽ ì§ë¶ ì²ëЬ ìì€í
ì
InsufficientFundsErrorë ìì²ë êžì¡ê³Œ ì¬ì© ê°ë¥í ìì¡ì ëí ìžë¶ ì 볎륌 ì ë¬í ì ììµëë€. - ëšìíë í ì€íž: ëšì ëë íµí© í ì€ížë¥Œ ìì±í ë ì ì ìë ì€ë¥ ì íì ì¬ì©í멎 ììëë 결곌륌 ìœê² ëšìží ì ììµëë€. í¹ì ì¬ì©ì ì ì ì€ë¥ì ë°ìì í¹ë³í í ì€ížíì¬ ì€ë¥ ì²ëЬ ë¡ì§ìŽ ìëí ëë¡ ìëíëì§ íìží ì ììµëë€.
- ë ëì API ëììž: API륌 ë žì¶íë ì í늬ìŒìŽì ì ê²œì° ì¬ì©ì ì ì ì€ë¥ ì íì ì€ë¥ë¥Œ ìë¹íë íŽëŒìŽìžížì ì ë¬íë 구조íëê³ ììž¡ ê°ë¥í ë°©ë²ì ì ê³µí©ëë€. ìŽë¥Œ íµíŽ ë³Žë€ ê°ë ¥í íµí©ê³Œ ì ìžê³ API ì¬ì©ììê² ë ëì ê°ë°ì 겜íì ì ê³µí©ëë€.
- êž°ì ë¶ì± ê°ì: ì¬ì ìë°©ì ìŽê³ ì 구조íë ì€ë¥ ì²ëЬë íŒëì€ëœê³ ëë²ê·žíêž° ìŽë €ìŽ ë¬žì ì ì¶ì ì ë°©ì§íì¬ ê¶ê·¹ì ìŒë¡ êž°ì ë¶ì±ë¥Œ ì€ìŽê³ ìœëë² ìŽì€ì ì¥êž°ì ìž ì ì§ êŽëЬì±ì í¥ììíµëë€.
Understanding TypeScript's Error Handling Foundation
TypeScriptë JavaScriptì Ʞ볞 ì€ë¥ ì²ëЬ ë©ì»€ëìŠì íì©íë©° ì£Œë¡ try...catch...finally ëžë¡ê³Œ Error ê°ì²Žë¥Œ ì¬ì©í©ëë€. JavaScriptì íì€ Error ê°ì²Žìë ëª ê°ì§ ì€ìí ìì±ìŽ ììµëë€.
message: ì€ë¥ì ëí ì¬ëìŽ ìœì ì ìë ì€ëª ì ëë€.name: ì€ë¥ ì íì ìŽëŠ(ì: 'Error', 'TypeError')ì ëë€.stack: ì€ë¥ê° ë°ìí ìì ì ížì¶ ì€íì í¬íšíë 묞ììŽì ëë€.
TypeScriptìì ìŒë° ì€ë¥ë¥Œ ë°ììí€ë©Ž ë€ì곌 ê°ìŽ ë³ŽìŒ ì ììµëë€.
function processData(data: any) {
if (!data || typeof data !== 'object') {
throw new Error('Invalid data provided. Expected an object.');
}
// ... process data
}
try {
processData(null);
} catch (error) {
console.error(error.message);
}
ìŽê²ì ìëíì§ë§ ì€ë¥ ë©ìì§ 'Invalid data provided. Expected an object.'ë ë§€ì° ìŒë°ì ì ëë€. ì¬ë¬ ì íì ì íšíì§ ìì ë°ìŽí°ê° ìë 겜ì°ìë ìŽë»ê² ë ê¹ì? ëëœë ë§€ê°ë³ìì ì못ë íìì ë§€ê°ë³ì륌 구ë³íŽìŒ íë 겜ì°ìë ìŽë»ê² ë ê¹ì?
Implementing Your First Custom Error Type
TypeScriptìì ì¬ì©ì ì ì ì€ë¥ ì íì ë§ëë ê°ì¥ ìŒë°ì ìŽê³ íšê³Œì ìž ë°©ë²ì Ʞ볞 ì ê³µ Error íŽëì€ë¥Œ íì¥íë ê²ì
ëë€. ìŽë¥Œ íµíŽ ì¬ì©ì ì ì ì€ë¥ê° íì€ ì€ë¥ ê°ì²Žì 몚ë ìì±ì ììíë ëìì ì첎 í¹ì ìì± ë° ë©ìë륌 ì¶ê°í ì ììµëë€.
Basic Custom Error Class
ë°ìŽí° ì íšì± ê²ì¬ 묞ì 륌 ëíëŽë ê°ëší ì¬ì©ì ì ì ì€ë¥ìž ValidationErrorë¶í° ììíê² ìµëë€.
class ValidationError extends Error {
constructor(message: string) {
super(message); // Call the parent constructor (Error)
this.name = 'ValidationError'; // Set the name of the error
// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, ValidationError);
}
}
}
Explanation:
extends ErrorìžValidationErroríŽëì€ë¥Œ ì ìí©ëë€.constructorëmessage묞ììŽì ì¬ì©íê³super()ížì¶ë¡ ì ë¬ë©ëë€. ìŽê²ì ë©ìì§ë¡ Ʞ볞ErroríŽëì€ë¥Œ ìŽêž°íí©ëë€.this.name = 'ValidationError'륌 ëª ìì ìŒë¡ ì€ì í©ëë€. ìŽê²ì Ʞ볞 'Error' ìŽëŠì ì¬ì ìíê³ ì¬ì©ì ì ì ì€ë¥ ì íì ëª ííê² ìë³íêž° ë묞ì ì¢ì ë°©ë²ì ëë€.Error.captureStackTrace(this, ValidationError)ì€ì ì€íìì ìì±ì ížì¶ ì첎륌 ì ìžíê³ ì¬ë°ë¥ž ì€í ì¶ì ì 캡ì²íë ë° ëììŽ ëë V8 í¹ì ìµì í(Node.js í겜ìì ìŒë°ì )ì ëë€. ìŽê²ì ì í ì¬íìŽì§ë§ ë ëì ëë²ê¹ ì ìíŽ ê¶ì¥ë©ëë€.
Throwing and Catching Custom Errors
ìŽì ìŽ ValidationError륌 ë°ììí€ê³ í¬ì°©íë ë°©ë²ì ìŽíŽë³Žê² ìµëë€.
function validateEmail(email: string): void {
if (!email || !email.includes('@')) {
throw new ValidationError('Invalid email format. Email must contain an "@" symbol.');
}
console.log('Email is valid.');
}
try {
validateEmail('test@example.com');
validateEmail('invalid-email');
} catch (error) {
if (error instanceof ValidationError) {
console.error(`Validation Error: ${error.message}`);
// You can perform specific actions for validation errors here
} else {
// Handle other unexpected errors
console.error(`An unexpected error occurred: ${error.message}`);
}
}
catch ëžë¡ìì instanceof ValidationError륌 ì¬ì©íì¬ ì¬ì©ì ì ì ì€ë¥ë¥Œ í¹ë³í ìë³íê³ ì²ëЬí©ëë€. ìŽë¥Œ íµíŽ ì°šë³íë ì€ë¥ ì²ëЬ ë¡ì§ì ì¬ì©í ì ììµëë€.
Adding Domain-Specific Properties to Custom Errors
ì¬ì©ì ì ì ì€ë¥ ì íì ì§ì í ê°ì ì ì¶ê°ì ìž ì»ší
ì€ížë³ ì 볎륌 ì ë¬íë êž°ë¥ìì ë¹ë¡¯ë©ëë€. ê°ì ì ì ìê±°ë ì í늬ìŒìŽì
(ì: InsufficientStockError)ì ëí ë³Žë€ ì êµí ì€ë¥ë¥Œ ë§ë€ìŽ ë³Žê² ìµëë€.
interface Product {
id: string;
name: string;
stock: number;
}
class InsufficientStockError extends Error {
public readonly productId: string;
public readonly requestedQuantity: number;
public readonly availableStock: number;
constructor(product: Product, requestedQuantity: number) {
const message = `Insufficient stock for product "${product.name}" (ID: ${product.id}). Requested: ${requestedQuantity}, Available: ${product.stock}.`;
super(message);
this.name = 'InsufficientStockError';
this.productId = product.id;
this.requestedQuantity = requestedQuantity;
this.availableStock = product.stock;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, InsufficientStockError);
}
}
}
// --- Usage Example ---
const productInStock: Product = {
id: 'p123',
name: 'Wireless Mouse',
stock: 5
};
function placeOrder(product: Product, quantity: number): void {
if (quantity > product.stock) {
throw new InsufficientStockError(product, quantity);
}
console.log(`Order placed successfully for ${quantity} of ${product.name}.`);
// ... update stock, process payment etc.
}
try {
placeOrder(productInStock, 3);
placeOrder(productInStock, 7); // This will throw InsufficientStockError
} catch (error) {
if (error instanceof InsufficientStockError) {
console.error(`Order failed: ${error.message}`);
console.error(`Details - Product ID: ${error.productId}, Requested: ${error.requestedQuantity}, Available: ${error.availableStock}`);
// Possible actions: Suggest alternative products, notify user, log for inventory management.
} else {
console.error(`An unexpected error occurred during order placement: ${error.message}`);
}
}
ìŽ ììì:
InsufficientStockErrorìë ì¶ê° ìì±ìžproductId,requestedQuantityë°availableStockìŽ ììµëë€.- ìŽë¬í ìì±ì ìì±ììì ìŽêž°íëìŽ ì€ë¥ì íšê» ì ë¬ë©ëë€.
- ì€ë¥ë¥Œ í¬ì°©í ë ìŽë¬í ìì±ì ì¡ìžì€íì¬ ë³Žë€ ììží íŒëë°±ì ì ê³µíê±°ë í¹ì 복구 ë¡ì§ì ížëŠ¬ê±°í ì ììµëë€. ì ìžê³ ì²ì€ìê² ìŽë¬í ìžë¶íë ì 볎ë ì§ì í ëë ìëíë ìì€í ìŽ ë€ìí ì§ììì 묞ì 륌 íšìšì ìŒë¡ ìŽíŽíê³ íŽê²°íë ë° ë§€ì° ì€ìí©ëë€.
Structuring Your Custom Error Hierarchy
ë í° ì í늬ìŒìŽì ì ê²œì° ì¬ì©ì ì ì ì€ë¥ ê³ìžµ 구조륌 ë§ëë ê²ìŽ ì ìµí ì ììµëë€. ìŽë¥Œ íµíŽ ë³Žë€ ì²Žê³ì ìŽê³ ê³ìžµíë ì€ë¥ ì²ëŠ¬ê° ê°ë¥í©ëë€.
ë€ìí ì íì API êŽë š ì€ë¥ê° ìë ìë늬ì€ë¥Œ ê³ ë €íììì€.
// Base API Error
class ApiError extends Error {
constructor(message: string) {
super(message);
this.name = 'ApiError';
if (Error.captureStackTrace) {
Error.captureStackTrace(this, ApiError);
}
}
}
// Specific API Errors inheriting from ApiError
class NetworkError extends ApiError {
public readonly statusCode?: number;
constructor(message: string, statusCode?: number) {
super(message);
this.name = 'NetworkError';
this.statusCode = statusCode;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, NetworkError);
}
}
}
class AuthenticationError extends ApiError {
constructor(message: string = 'Authentication failed. Please check your credentials.') {
super(message);
this.name = 'AuthenticationError';
if (Error.captureStackTrace) {
Error.captureStackTrace(this, AuthenticationError);
}
}
}
class ResourceNotFoundError extends ApiError {
public readonly resourceId: string;
constructor(resourceId: string, message: string = `Resource with ID "${resourceId}" not found.`) {
super(message);
this.name = 'ResourceNotFoundError';
this.resourceId = resourceId;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, ResourceNotFoundError);
}
}
}
// --- Usage Example ---
async function fetchUserData(userId: string): Promise<any> {
const response = await fetch(`/api/users/${userId}`);
if (!response.ok) {
if (response.status === 401) {
throw new AuthenticationError();
} else if (response.status === 404) {
throw new ResourceNotFoundError(userId);
} else {
throw new NetworkError(`API request failed with status ${response.status}`, response.status);
}
}
return response.json();
}
try {
const user = await fetchUserData('user123');
console.log('User data:', user);
} catch (error) {
if (error instanceof AuthenticationError) {
console.error('Authentication Error:', error.message);
// Redirect to login page globally.
} else if (error instanceof ResourceNotFoundError) {
console.error('Resource Not Found:', error.message);
// Inform user that the requested resource is unavailable.
} else if (error instanceof NetworkError) {
console.error(`Network Error: ${error.message} (Status: ${error.statusCode})`);
// Potentially retry the request or inform the user about connection issues.
} else {
console.error('An unknown API error occurred:', error.message);
}
}
ìŽ ê³ìžµ 구조ìì:
ApiErrorë 몚ë API êŽë š 묞ì ì ëí ê³µíµ êž°ë° ìí ì í©ëë€.NetworkError,AuthenticationErrorë°ResourceNotFoundErrorëApiErrorìì ììëìŽ ê° ì íì í¹ì íê² ì²ëЬí ì ììµëë€.- catch ëžë¡ì 뚌ì ê°ì¥ 구첎ì ìž ì€ë¥(ì:
AuthenticationError)륌 íìží ë€ì íìí ê²œì° ë ìŒë°ì ìž ì€ë¥(ì:ApiError)ë¡ ë첎í ì ììµëë€. ìŽë ë€ìí ì§ììì ìžìŠì ìí¥ì 믞ì¹ë ë€ìí ë€ížìí¬ ìì ì± ëë ê·ì ì구 ì¬íì ê°ì§ ì ìë êµì ì í늬ìŒìŽì ì ë§€ì° ì€ìí©ëë€.
Best Practices for Implementing Custom Error Types
ì¬ì©ì ì ì ì€ë¥ ì íì ìŽì ì ê·¹ëííë €ë©Ž ë€ì ëªšë² ì¬ë¡ë¥Œ ê³ ë €íììì€.
- 구첎ì ìŒë¡ ì§ì íììì€: ì€ë¥ íŽëì€ ìŽëŠì ëª ííê³ ì€ëª ì ìŒë¡ ì§ì íììì€. ìŽëŠ ì첎ë ì€ë¥ì í¹ì±ì ì ë¬íŽìŒ í©ëë€.
Errorìì ììíììì€: íì Ʞ볞 ì ê³µErroríŽëì€ë¥Œ íì¥íì¬ ì¬ì©ì ì ì ì€ë¥ê° íì€ JavaScript ì€ë¥ì²ëŒ ëìíê³messageë°stack곌 ê°ì íìí ìì±ì ê°ëë¡ íììì€.nameìì± ì€ì :this.nameì ì¬ì©ì ì ì ì€ë¥ íŽëì€ ìŽëŠìŒë¡ ëª ìì ìŒë¡ ì€ì íììì€. ìŽë ë°íì ì€ì ìë³íë ë° ë§€ì° ì€ìí©ëë€.- êŽë š ë°ìŽí° í¬íš: 컚í ì€ížë¥Œ ì ê³µíê³ ëë²ê¹ ëë 복구륌 ì©ìŽíê² íë ìì±ì ì¬ì©ì ì ì ì€ë¥ì ì¶ê°íììì€. ê°ë°ì ëë ìëíë ìì€í ìŽ ë¬žì 륌 ìŽíŽíê³ íŽê²°íêž° ìíŽ ìŽë€ ì ë³Žê° íìíì§ ìê°íŽë³Žììì€.
- ì€ë¥ 묞ìí: ìœëì ë§ì°¬ê°ì§ë¡ ì¬ì©ì ì ì ì€ë¥ ì íë 묞ìííŽìŒ í©ëë€. ê° ì€ë¥ê° 묎ìì ì믞íëì§, ìŽë€ ìì±ì ì ë¬íëì§, ìžì ë°ìí ì ìëì§ë¥Œ ì€ëª íììì€. ìŽë í¹í ì ìžê³ì ë¶ì°ë íìê² ì€ìí©ëë€.
- ìŒêŽë ë°ì ë° í¬ì°©: í ëŽìì ì€ë¥ë¥Œ ë°ììí€ë ë°©ë²ê³Œ ìì¹, ì€ë¥ë¥Œ í¬ì°©íê³ ì²ëЬíë ë°©ë²ì ëí ê·ì¹ì ì€ì íììì€. ìŽë¬í ìŒêŽì±ì ë¶ì° í겜ìì ì€ë¥ êŽëЬì ëí íµìŒë ì ê·Œ ë°©ìì ì€ìí©ëë€.
- ê³Œì© ë°©ì§: ì¬ì©ì ì ì ì€ë¥ê° ê°ë ¥íì§ë§ ì¬ìí ë¶ížì ëíŽ ì€ë¥ë¥Œ ë§ë€ì§ ë§ììì€. í¹ì ì²ëŠ¬ê° íìíê±°ë ì€ìí 컚í ì€íž ì 볎륌 ì ë¬íë ëë ·í ì€ë¥ 조걎ì ì¬ì©íììì€.
- ì€ë¥ ìœë ê³ ë €: ë€ìí ìžìŽ ëë íë«íŒìì íë¡ê·žëë° ë°©ììŒë¡ ì€ë¥ ìë³ìŽ íìí ìì€í ì ê²œì° ì«ì ëë 묞ììŽ ì€ë¥ ìœë륌 ì¬ì©ì ì ì ì€ë¥ ì íì ì¶ê°íë ê²ì ê³ ë €íììì€. ìŽë í¹ì ì§ì êž°ì¬ë¡ ì€ë¥ë¥Œ íì§ííê±°ë ë§€ííë ë° ì ì©í ì ììµëë€.
- ì€ì ì§ì€ì ì€ë¥ ì²ëЬ: ë í° ì í늬ìŒìŽì ììë ì€ë¥ë¥Œ ê°ë¡ì±ì ì²ëЬíë ì€ì ì§ì€ì ì€ë¥ ì²ëЬ 몚ë ëë ìë¹ì€ë¥Œ ê³ ë €íì¬ ì í늬ìŒìŽì ì ì¬ë¬ ë¶ë¶ìì ìŒêŽë ë¡ê¹ , ë³Žê³ ë° ì ì¬ì ìŒë¡ ì¬ì©ì íŒëë°± ë©ì»€ëìŠì 볎ì¥í©ëë€. ìŽë êžë¡ë² ì í늬ìŒìŽì ì ì€ìí íšíŽì ëë€.
Global Considerations and Localization
êžë¡ë² ëìì ìíŽ ê°ë°í ë ì€ë¥ ë©ìì§ ì첎(message ìì±)ë ì ì€íê² ê³ ë €íŽìŒ í©ëë€.
- ì€ë¥ ë©ìì§ ë¬žììŽì ì§ì íì§ííì§ ë§ììì€: ì€ë¥ íŽëì€ì íì§íë ë©ìì§ë¥Œ íë ìœë©íë ëì ì¬ì©ì ë¡ìŒìŒ ëë ì í늬ìŒìŽì
ì€ì ì êž°ë°ìŒë¡ íì§íë ë©ìì§ë¥Œ ê²ìíëë¡ ìì€í
ì ì€ê³íììì€. ì¬ì©ì ì ì ì€ë¥ë íì§í ìë¹ì€ê° ì¬ì©í ì ìë
errorCodeëëkey륌 ì ë¬í ì ììµëë€. - ê°ë°ì ì€ì¬ ë©ìì§ì ì§ì€íììì€: ì€ë¥ ê°ì²Ž ëŽì ììží ì€ë¥ ë©ìì§ì 죌ì ëìì ìŒë°ì ìŒë¡ ê°ë°ìì ëë€. ë°ëŒì ìŽë¬í ë©ìì§ê° ëª ííê³ ê°ê²°íë©° êž°ì ì ìŒë¡ ì ííì§ íìžíììì€. ì¬ì©ììê² íìëë ì€ë¥ ë©ìì§ë ë³ëë¡ ì²ëЬíê³ ì¬ì©ì ì¹íì ìŽë©° íì§íëìŽìŒ í©ëë€.
- êµì 묞ì ì§í©: ì¬ì©ì ì ì ì€ë¥ ëŽì 몚ë 묞ììŽ ìì±ìŽ êµì 묞ì ì§í©ì ì¬ë°ë¥Žê² ì²ëЬí ì ìëì§ íìžíììì€. TypeScript ë° JavaScriptì íì€ ë¬žììŽ ì²ëЬë ìŒë°ì ìŒë¡ ì ëìœë륌 ì ì§ìí©ëë€.
ì륌 ë€ìŽ ì¬ì©ì ì ì ì€ë¥ë ë€ì곌 ê°ìŽ ë³ŽìŒ ì ììµëë€.
class UserNotFoundError extends Error {
public readonly userId: string;
public readonly errorCode: string = 'ERR_USER_NOT_FOUND'; // For localization/lookup
constructor(userId: string, message: string = 'User not found.') {
super(message); // Default message, can be overridden or looked up.
this.name = 'UserNotFoundError';
this.userId = userId;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, UserNotFoundError);
}
}
}
// In a localization service:
function getLocalizedErrorMessage(error: Error & { errorCode?: string }, locale: string): string {
if (!error.errorCode) {
return error.message;
}
const messages: { [key: string]: { [key: string]: string } } = {
'en-US': {
'ERR_USER_NOT_FOUND': `User with ID ${ (error as any).userId } could not be found.`
},
'es-ES': {
'ERR_USER_NOT_FOUND': `No se encontró al usuario con ID ${ (error as any).userId }.`
}
// ... other locales
};
return messages[locale]?.[error.errorCode] || error.message;
}
// Usage:
try {
// ... attempt to find user
throw new UserNotFoundError('abc-123');
} catch (error) {
if (error instanceof UserNotFoundError) {
const userMessage = getLocalizedErrorMessage(error, 'es-ES');
console.error(`Error: ${userMessage}`); // Displays Spanish message
} else {
console.error(`Generic error: ${error.message}`);
}
}
Conclusion
TypeScriptìì ì¬ì©ì ì ì ì€ë¥ ì íì 구ííë ê²ì ëšìí ì°ìí ìœë© ë°©ìì 묞ì ê° ìëëë€. í¹í ì ìžê³ 컚í
ì€ížìì ì í늬ìŒìŽì
ì ê²¬ê³ ì±, ì ì§ êŽëŠ¬ì± ë° ê°ë°ì 겜íì í¬ê² í¥ììí€ë ì ëµì ê²°ì ì
ëë€. Error íŽëì€ë¥Œ íì¥íšìŒë¡ìš ëë²ê¹
ì ê°ìííê³ ì€ë¥ ì²ëЬì ëí ìžë¶íë ì ìŽë¥Œ ê°ë¥íê² íë©° ê·ì€í ëë©ìžë³ 컚í
ì€ížë¥Œ ì ê³µíë í¹ì ì 볎 ì ê³µì ìŽê³ ì€í ê°ë¥í ì€ë¥ ê°ì²Žë¥Œ ë§ë€ ì ììµëë€.
ë€ìí êµì ëìì ëììŒë¡ íë ì êµí ì í늬ìŒìŽì ì ê³ì 구ì¶íšì ë°ëŒ ì ì ìë ì¬ì©ì ì ì ì€ë¥ ì ëµì í¬ìí멎 볎ìì ë°ì ì ììµëë€. ê°ë° í ëŽìì ë ëª íí 컀뮀ëìŒìŽì , ë íšìšì ìž ë¬žì íŽê²°, ê¶ê·¹ì ìŒë¡ ì ìžê³ ì¬ì©ì륌 ìí ë³Žë€ ìì ì ìž ìíížìšìŽë¡ ìŽìŽì§ëë€. ì¬ì©ì ì ì ì€ë¥ì íì ë°ìë€ìŽê³ TypeScript ê°ë°ì í ëšê³ ëìŽì¬ëЬììì€.