I'm trying to integrate my strapi application with sentry, for which I will need to write a middleware. Use the following documentation: https://strapi.io/documentation/3.0.0-beta.x/advanced/middlewares.html I was able to create a custom middleware with the following:
module.exports = strapi => {
return {
initialize() {
strapi.app.use(async (ctx, next) => {
try {
await next();
} catch (error) {
Sentry.captureException(error)
}
});
}
};
};
However, doing so is preventing strapi to print out the errors to console the usual way but the error is captured by sentry application.
So, my question is: How do I capture the error "seamlessly" and send it to a third party application, at the same time not hinder with the default functioning and error logging of the strapi to console.
Any help would be greatly appreciated!
Thanks :)
EDIT: I figured out that all strapi errors are accessible at the "boom" middleware as pointed out in this file: https://github.com/strapi/strapi/blob/6309af25c921640cb76aeeda463e55db1eb53ef1/packages/strapi/lib/middlewares/boom/index.js#L69
Answer has been given by the authors here: https://github.com/strapi/strapi/issues/4071
Related
This is the screenshot of my Javascript codeThis is the screenshot of postman
app.get("/students/:id",async(req,res)=>{
try {
const _id=req.params.id;
const studentData=await Student.findById(_id);
console.log(studentData);
if(!studentData){
return res.status(404).send();
}else
res.send(studentData);
}
catch(err) {
res.send(err);
}
});
For this id case I am able to find the value in database.
But if I am using model.find({name:name}) as per screenshot ,I am getting some error in postman.Can anyone help me to solve the issue.
it seems you have two conflicting routes, from your code above I see you have a get route for /student/:id and from the screenshot you have another get route for /student/:name these two routes cannot exist together as express is going to pick the first one you defined.
I suggest changing them to /student/id/:id and /student/name/:name.
The following URL provides a pretty good walkthrough of how to wire up a node/express implementation to read from Google Cloud Platform Cloud SQL:
https://medium.com/#austinhale/building-a-node-api-with-express-and-google-cloud-sql-9bda260b040f
I implemented the steps in this article and my local implementation is working as expected. However, this URL doesn't cover how to wire up inserts/updates. Based on some googling, I came up with the following implementation for a post/insert:
// POST method route
app.post('/users', function (req, res) {
var post = { FirstName: req.FirstName, LastName: req.LastName };
var query = connection.query('INSERT INTO User SET ?', post, function (error, results, fields) {
if (error){
console.log(error.message);
}
});
})
I'm POST-ing the following request from Postman as raw JSON:
http://localhost:3000/users/
{
"FirstName":"John",
"LastName":"Smith"
}
However, the response status is 404 Not Found. The standard GET is working as expected though. Any idea what I might be doing wrong here? Also, I'm new to Node/Express. What's the easiest way to get started debugging? For example, is there a recommended plugin for CDT that I can use for this? The sample code that I used for the GET used console.log("message") but when I tried this approach, nothing appeared to be written out to the node console window or to CDT?
I'm walking through the Javascript demos of pg-promise-demo and I have a question about the route /api/users/:name.
Running this locally works, the user is entered into the database, but is there a reason this wouldn't be a POST? Is there some sort of advantage to creating a user in the database using GET?
// index.js
// --------
app.get('/api/users/:name', async (req, res) => {
try {
const data = (req) => {
return db.task('add-user', async (t) => {
const user = await t.users.findByName(req.params.name);
return user || t.users.add(req.params.name);
});
};
} catch (err) {
// do something with error
}
});
For brevity I'll omit the code for t.users.findByName(name) and t.users.add(name) but they use QueryFile to execute a SQL command.
EDIT: Update link to pg-promise-demo.
The reason is explained right at the top of that file:
IMPORTANT:
Do not re-use the HTTP-service part of the code from here!
It is an over-simplified HTTP service with just GET handlers, because:
This demo is to be tested by typing URL-s manually in the browser;
The focus here is on a proper database layer only, not an HTTP service.
I think it is pretty clear that you are not supposed to follow the HTTP implementation of the demo, rather its database layer only. The demo's purpose is to teach you how to organize a database layer in a large application, and not how to develop HTTP services.
I'm newbie in Sails JS..
I'm trying to create a new custom response, I didn't find any auto-generator so I created it manually.
//someName.js
module.exports = function someName(data, options) {
console.log('test');
};
I'm trying to access this response from controller:
//someController.js
module.exports = {
someController: function(req, res) {
res.someName();
}
}
The problem is that WebStorm isn't recognize this response..
Unresolved function or method someName.
But when executing the app-it's working..(WebStorm recognize the default responses which came with the 'sails new someApp').
Thanks for your help!
The code in the Sails.js library that loads custom responses probably does something like this:
files = getFilesInApiResponsesDirectory()
files.forEach(function(file) {
name = extractName(file);
res[name] = require(file); // <-- too dynamic to infer at this point
})
There is no way that WebStorm code analyzer could infer the relationship between res and your custom response function without actually running Sails.js code or receiving a (less dynamic, more explicit) hint.
Anyway, the message you got does not necessarily represent a critical error, it's a "code inspection" you can disable or suppress.
i'm fairly new to node.js so this could potentially a total noob question. Anyway. I discovered the mean.io Project. In the official article-example on Github, there is the following method in the article-controller.
exports.update = function(req, res) {
var article = req.article;
article = _.extend(article, req.body);
article.save(function(err) {
if (err) {
return res.jsonp(500, {
error: 'Cannot update the article'
});
}
res.jsonp(article);
});
};
With a corresponding route
module.exports = function(Articles, app, auth) {
app.route('/articles')
.get(articles.all)
.post(auth.requiresLogin, articles.create);
app.route('/articles/:articleId')
.get(articles.show)
.put(auth.requiresLogin, hasAuthorization, articles.update)
.delete(auth.requiresLogin, hasAuthorization, articles.destroy);
// Finish with setting up the articleId param
app.param('articleId', articles.article);
};
So I'm confused. When and where does the route pass the req/res parameters to the articles.update, or any other articles function? Is there some hidden mechanism in node/express/mean I've missed out?
Thanks in advance.
app.route('/articles/:articleId')
.get(articles.show);
This means express will invoke articles.show method with request and response as first two parameters when a GET request comes with matching path
.