{
class PropertyRequiredError extends Error {
property: string;
constructor(property: string) {
super("No property" + property);
this.name = this.constructor.name;
this.property = property;
}
}
class ValidationError extends Error {
constructor(message: string) {
super(message);
this.name = this.constructor.name;
}
}
function readUser(json: string) {
let user = JSON.parse(json);
if (!user.age) {
throw new PropertyRequiredError("age");
}
if (!user.name) {
throw new PropertyRequiredError("name");
}
return true;
}
function parseSomething(): boolean | ValidationError | PropertyRequiredError {
const data: string = '{"age":25}';
try {
return readUser(data);
} catch (e) {
if (e instanceof ValidationError) {
console.error("name: " + e.name);
console.error("message: " + e.message);
console.error("stack: " + e.stack);
}
if (e instanceof PropertyRequiredError) {
console.error("name: " + e.name);
console.error("message: " + e.message);
console.error("stack: " + e.stack);
} else {
throw e;
}
return false;
}
}
parseSomething();
}