/
spirzen
/
it-code-examples
Обзор
Документация
Войти
/
spirzen
/
it-code-examples
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
examples/javascript/js-501-22-042/main.js
54 строки
2 KB
Spirzen
Code migration pack
09 июн 2026, 01:41
09 июн 2026, 01:41
cebc284
Код
Авторство
О чём код?
class ValidationError extends Error { constructor(message, field) { super(message); this.name = "ValidationError"; this.field = field; this.timestamp = new Date().toISOString(); } } class DatabaseError extends Error { constructor(message, query, code) { super(message); this.name = "DatabaseError"; this.query = query; this.code = code; this.timestamp = new Date().toISOString(); } } class AuthenticationError extends Error { constructor(message, userId = null) { super(message); this.name = "AuthenticationError"; this.userId = userId; this.timestamp = new Date().toISOString(); } } // Использование function validateUser(user) { if (!user.name || user.name.length < 2) { throw new ValidationError("Имя должно содержать минимум 2 символа", "name"); } if (!user.email || !user.email.includes("@")) { throw new ValidationError("Некорректный email", "email"); } if (user.age < 0 || user.age > 150) { throw new ValidationError("Некорректный возраст", "age"); } return true; } try { validateUser({ name: "А", email: "invalid", age: 200 }); } catch (error) { if (error instanceof ValidationError) { console.log(`${error.name}: ${error.message}`); console.log(`Поле: ${error.field}`); console.log(`Время: ${error.timestamp}`); } }