From fb7c68f9f3c01c24b7bd560516589f926f87be97 Mon Sep 17 00:00:00 2001 From: Miguel Beltran Date: Tue, 14 May 2024 08:35:03 +0200 Subject: [PATCH] expanded the express-example --- examples/express-sample/README.md | 11 ++++--- examples/express-sample/app.js | 14 +-------- examples/express-sample/logger.js | 16 ++++++++++ examples/express-sample/routes/index.js | 40 +++++++++++++++++++++++-- examples/express-sample/views/index.ejs | 3 ++ examples/express-sample/views/send.ejs | 18 +++++++++++ 6 files changed, 82 insertions(+), 20 deletions(-) create mode 100644 examples/express-sample/logger.js create mode 100644 examples/express-sample/views/send.ejs diff --git a/examples/express-sample/README.md b/examples/express-sample/README.md index 7774d7d..2c90fed 100644 --- a/examples/express-sample/README.md +++ b/examples/express-sample/README.md @@ -24,9 +24,12 @@ in the subdirectory where you found this README.md file. ## Interesting files to look +- `logger.js` + - Setup of Raygun (lines 9-14) - `app.js` - - Setup of Raygun (lines 9-12) - - Sets the user (lines 27-29) - - Attaches Raygun to Express (line 60) + - Sets the user (lines 17-19) + - Attaches Raygun Breadcrumb middleware to Express (line 26) + - Attaches Raygun to Express (line 53) - `routes/index.js` - - Tries to use a fake object, which bounces up to the Express handler (lines 11-15) + - `/send` endpoint: Sends a custom error to Raygun (lines 11-34) + - `/error` endpoint: Tries to use a fake object, which bounces up to the Express handler (lines 36-49) diff --git a/examples/express-sample/app.js b/examples/express-sample/app.js index bbb404a..4e47425 100644 --- a/examples/express-sample/app.js +++ b/examples/express-sample/app.js @@ -1,17 +1,4 @@ -var config = require("config"); -if (config.Raygun.Key === "YOUR_API_KEY") { - console.error( - "[Raygun4Node-Express-Sample] You need to set your Raygun API key in the config file", - ); - process.exit(1); -} - -// Setup Raygun -var raygun = require("raygun"); -var raygunClient = new raygun.Client().init({ - apiKey: config.Raygun.Key, -}); var express = require("express"); var path = require("path"); @@ -19,6 +6,7 @@ var logger = require("morgan"); var cookieParser = require("cookie-parser"); var bodyParser = require("body-parser"); var sassMiddleware = require("node-sass-middleware"); +var raygunClient = require("./logger"); var routes = require("./routes/index"); var users = require("./routes/users"); diff --git a/examples/express-sample/logger.js b/examples/express-sample/logger.js new file mode 100644 index 0000000..3ce73c4 --- /dev/null +++ b/examples/express-sample/logger.js @@ -0,0 +1,16 @@ +var config = require("config"); + +if (config.Raygun.Key === "YOUR_API_KEY") { + console.error( + "[Raygun4Node-Express-Sample] You need to set your Raygun API key in the config file", + ); + process.exit(1); +} + +// Setup Raygun +var raygun = require("raygun"); +var raygunClient = new raygun.Client().init({ + apiKey: config.Raygun.Key, +}); + +module.exports = raygunClient; diff --git a/examples/express-sample/routes/index.js b/examples/express-sample/routes/index.js index 523d931..f7c0c39 100644 --- a/examples/express-sample/routes/index.js +++ b/examples/express-sample/routes/index.js @@ -1,14 +1,48 @@ -var express = require("express"); -var router = express.Router(); +const express = require("express"); +const router = express.Router(); +const raygunClient = require("../logger"); /* GET home page. */ router.get("/", function (req, res, next) { res.render("index", { - title: "Express", + title: "Raygun Express Example", }); }); +router.get("/send", function (req, res, next) { + + raygunClient.addBreadcrumb({ + level: "debug", + category: "Example", + message: "Breadcrumb in /send endpoint", + customData: { + "custom-data": "data", + } + }); + + raygunClient.send("Error in /send endpoint").then((message) => { + res.render("send", { + title: "Sent custom error to Raygun", + body: `Raygun status code: ${message.statusCode}`, + }); + }).catch((error) => { + res.render("send", { + title: "Failed to send custom error to Raygun", + body: error.toString(), + }); + }) +}); + router.get("/error", function (req, res, next) { + raygunClient.addBreadcrumb({ + level: "debug", + category: "Example", + message: "Breadcrumb in /error endpoint", + customData: { + "custom-data": "data", + } + }); + // Call an object that doesn't exist to send an error to Raygun fakeObject.FakeMethod(); res.send(500); diff --git a/examples/express-sample/views/index.ejs b/examples/express-sample/views/index.ejs index a5ef3e5..9f3d474 100644 --- a/examples/express-sample/views/index.ejs +++ b/examples/express-sample/views/index.ejs @@ -8,6 +8,9 @@

<%= title %>

Welcome to <%= title %>

+
+ Send a custom error +
Throw an error
diff --git a/examples/express-sample/views/send.ejs b/examples/express-sample/views/send.ejs new file mode 100644 index 0000000..09be1db --- /dev/null +++ b/examples/express-sample/views/send.ejs @@ -0,0 +1,18 @@ + + + + <%= title %> + + + +

<%= title %>

+

<%= title %>

+
+ <%= body %> +
+ +
+ Back to index +
+ +