Created
August 26, 2018 08:55
-
-
Save sj82516/ffa9be6fe860b73fb5130d5aa5c9cd1f 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
const Koa = require('koa'); | |
const app = new Koa(); | |
app.use(async (ctx, next) => { | |
try { | |
await next(); | |
}catch(error){ | |
console.error(error); | |
ctx.body = "ServerError"; | |
} | |
}); | |
app.on('error', (err, ctx) => { | |
console.log("因為有中間件統一處理了Error,這裡理論上不會觸發") | |
}); | |
// response | |
app.use(ctx => { | |
throw new Error("Error"); | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment