New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
validation error broken? maybe related to serverless-offline / serverless-webpack plugin? #94
Comments
Is this what you're talking about 🤔 |
@vladgolubev Yes, thank you! Just tried the example middleware from the first post and it gave me more of what I expected. I had skimmed the issue and comments but missed the little line with my error message. Fried from a long day at work but I'll review that thread in detail tomorrow. Thanks again :) |
Hello @dwelch2344, thanks for your submission. Since you are the second person affected by this, I was trying to spend some time yesterday to replicate your issue and investigate it further, but I wasn't able to reproduce the case. I'd like to see your full code implementation (the one you posted seems more like a draft rather than the real implementation). Would you be ok with posting it? |
I have this error too when using serverless-offline-plugin, and can reproduce with his code |
Infact, it does this in AWS as well, it sends a plaintext message, not a JSON string |
This is my implementation, its not perfect, I still need to go through things... const inputSchema = {
required: [ 'name', 'email', 'password' ],
properties: {
name: { type: 'string' },
email: { type: 'string', pattern: '/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/' },
password: { type: 'string' },
},
};
const userCreate = async (evt, ctx) => {
ctx.callbackWaitsForEmptyEventLoop = false;
let { name, email, password } = evt.body;
const salt = await bcrypt.genSalt();
const hash = await bcrypt.hash(password, salt);
password = hash;
try {
mongoose.connect(MONGO_URL);
} catch (err) {
return { statusCode: 501, body: JSON.stringify({ message: err || 'Internal server error.' })};
}
try {
const userExists = await UserModel.find({ email }).limit(1);
if (userExists.length === 0) {
const newUser = new UserModel({ ...body, salt });
await newUser.save();
mongoose.connection.close();
return { statusCode: 200, body: JSON.stringify({ name, email })};
} else {
return { statusCode: 404, body: JSON.stringify({ message: 'User exists' })};
}
} catch (err) {
mongoose.connection.close();
return { statusCode: 501, body: JSON.stringify({ message: err || 'Internal server error.' })};
}
};
const handler = middy(userCreate)
.use(validator({ inputSchema }))
.use(urlEncodeBodyParser())
.use(httpErrorHandler());
module.exports = { handler }; |
I've done some digging around, this is an issue caused by httpsErrorHandler middleware. Its an easy fix. Give the option to switch between a JSON error, or a plain-text body. For now I will make my own error solution that is similar to that of httpErrorHandler. |
Just getting started and can't seem to get validation error messages working based on the README. We're using the es7 webpack plugin + serverless-offline, but I don't think that is why.
No matter what we do, we always get a strange
Unexpected 'E'
response body.Tried debugging, going as far to the actual source and making sure all our middleware is hit in the correct order and everything, but no dice.
Anyone able to take a look at / spot a stupid mistake we're making? Thanks in advance
handler.js
serverless.yml
The text was updated successfully, but these errors were encountered: