Skip to content

Instantly share code, notes, and snippets.

@rkhaslarov
Last active February 16, 2022 16:28
Show Gist options
  • Save rkhaslarov/8683cfb97cf1354c52891e3e341cef1c to your computer and use it in GitHub Desktop.
Save rkhaslarov/8683cfb97cf1354c52891e3e341cef1c to your computer and use it in GitHub Desktop.
try {
// your code is here
} catch(e) {
// in case of exception, handle it here
}
try {
await authenticate({});
} catch(e) {
console.error('Unexpected error:', e.message)
}
class ValidationError extends Error {
constructor(message) {
super(message);
this.name = this.constructor.name;
}
}
function authenticate(request) {
try {
const { token } = request.query ?? {};
if (!token) {
throw new ValidationError ('Have not received [token]');
}
} catch(e) {
console.log('Failed to authenticate', e.name, e.message)
}
}
authenticate({});
class ValidationError extends Error {
constructor(message) {
super(message);
this.name = this.constructor.name;
}
}
function authenticate(request) {
try {
const { token } = request.query ?? {};
if (!token) {
throw new ValidationError('have not received [token]');
}
} catch(e) {
if (e instanceof ValidationError) {
console.log('Could not pass gatekeeper:', e.message)
} else {
console.log('Failed to authenticate', e.name, e.message)
}
}
}
authenticate({});
try {
throw new Error('The most unexpected error in the world!');
} catch(e) {
console.log(e.name); // Error Type: in this case just Error
console.log(e.message); // Error Message: The string we’ve passed as an argument to the error constructor in the try block
console.log(e.stack); // Error Stack: The calls sequence that led to the exception
}
try {
const { token } = request.query;
if (!token) {
throw new Error('Have not received [token]');
}
} catch(e) {
console.log('Failed to authenticate', e.messsage)
}
try {
// your code is here
} catch(e) {
// in case of exception, handle it here
} finally {
// regardless of the result, this block is executed
}
class ValidationError extends Error {
constructor(message) {
super(message);
this.name = this.constructor.name;
}
}
function authenticate(request) {
try {
const { token } = request.query ?? {};
if (!token) {
throw new ValidationError('have not received [token]');
}
} catch(e) {
if (e instanceof ValidationError) {
console.log('Could not pass gatekeeper:', e.message)
} else {
throw error; // retrow the error futher
}
}
}
authenticate({});
class ValidationError extends Error {
constructor(message) {
super(message);
this.name = this.constructor.name;
}
}
function validateRequest(request) {
if (!token) {
throw new ValidationError('have not received [token]');
}
}
function authenticate(request) {
try {
validateRequest(request);
// further processing...
} catch(e) {
if (e instanceof ValidationError) {
console.log('Could not pass gatekeeper:', e.message)
} else {
throw error;
}
}
}
try {
authenticate({});
} catch(e) {
console.error('Unexpected error:', e.message)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment