Last active
February 16, 2022 16:28
-
-
Save rkhaslarov/8683cfb97cf1354c52891e3e341cef1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
// your code is here | |
} catch(e) { | |
// in case of exception, handle it here | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
await authenticate({}); | |
} catch(e) { | |
console.error('Unexpected error:', e.message) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
const { token } = request.query; | |
if (!token) { | |
throw new Error('Have not received [token]'); | |
} | |
} catch(e) { | |
console.log('Failed to authenticate', e.messsage) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
// your code is here | |
} catch(e) { | |
// in case of exception, handle it here | |
} finally { | |
// regardless of the result, this block is executed | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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