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
const a = 5 | |
try { | |
console.log(b) // b is not defined, so throws an error | |
} catch (err) { | |
console.error(err) // will log the error with the error stack | |
} | |
console.log(a) // still gets 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
const a = 5 | |
try { | |
console.log(b) // b is not defined, so throws an error | |
} catch (err) { | |
console.error(err) // will log the error with the error stack | |
} finally { | |
console.log(a) // will always get 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
myAsyncFunc(someInput, (err, result) => { | |
if(err) return console.error(err) // we will see later what to do with the error object. | |
console.log(result) | |
}) |
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
Promise.resolve(1) | |
.then(res => { | |
console.log(res) // 1 | |
throw new Error('something went wrong') | |
return Promise.resolve(2) | |
}) | |
.then(res => { | |
console.log(res) // will not get 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
;(async function() { | |
try { | |
await someFuncThatThrowsAnError() | |
} catch (err) { | |
console.error(err) // we will make sense of that later | |
} | |
console.log('Easy!') // will get 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 CustomError extends Error { | |
constructor(code = 'GENERIC', status = 500, ...params) { | |
super(...params) | |
if (Error.captureStackTrace) { | |
Error.captureStackTrace(this, CustomError) | |
} | |
this.code = code | |
this.status = status |
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
const express = require('express') | |
const router = express.Router() | |
const CustomError = require('../CustomError') | |
router.use(async (req, res) => { | |
try { | |
const route = require(`.${req.path}`)[req.method] | |
try { | |
const result = route(req) // We pass the request to the route function |
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
const CustomError = require('../CustomError') | |
const GET = req => { | |
// example for success | |
return { name: 'Rio de Janeiro' } | |
} | |
const POST = req => { | |
// example for unhandled error | |
throw new Error('Some unexpected error, may also be thrown by a library or the runtime.') |
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
import React, { Component } from 'react' | |
import GlobalError from './GlobalError' | |
class Application extends Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
error: '', |
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
import React, { Component } from 'react' | |
class GlobalError extends Component { | |
render() { | |
if (!this.props.error) return null | |
return ( | |
<div | |
style={{ | |
position: 'fixed', |
OlderNewer