Run ❯
Get your
own Node
server
❯
Run Code
Ctrl+Alt+R
Change Orientation
Ctrl+Alt+O
Change Theme
Ctrl+Alt+D
Go to Spaces
Ctrl+Alt+P
// @ts-check class ValidationError extends Error { /** @type {string | undefined} */ field; /** * @param {string} message * @param {string=} field */ constructor(message, field) { super(message); this.name = 'ValidationError'; this.field = field; Object.setPrototypeOf(this, ValidationError.prototype); } } class DatabaseError extends Error { /** @type {number} */ code; /** * @param {string} message * @param {number} code */ constructor(message, code) { super(message); this.name = 'DatabaseError'; this.code = code; Object.setPrototypeOf(this, DatabaseError.prototype); } } /** @param {{name?: string, email?: string}} user */ function validateUser(user) { if (!user.name) { throw new ValidationError('Name is required', 'name'); } if (!user.email || !user.email.includes('@')) { throw new ValidationError('Invalid email format', 'email'); } } try { validateUser({ email: 'no-at-symbol' }); } catch (error) { if (error instanceof ValidationError) { console.log(`Validation error in ${error.field}: ${error.message}`); } } try { validateUser({ name: 'Alice', email: 'not-an-email' }); } catch (error) { if (error instanceof ValidationError) { console.log(`Validation error in ${error.field}: ${error.message}`); } }
Validation error in name: Name is required Validation error in email: Invalid email format