Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const expressValidation = require('express-validation')
const app = {
use: function (next) {
middleware = next
}
}
error({
app
})
const errors = {}
const options = {
status: 400
}
const errorThrow = new expressValidation.ValidationError(errors, options)
const req = {}
const res = {
status: () => {}
}
const status = sinon.stub(res, 'status').returns({
json: () => {}
})
// simulando a chamada do middleware
middleware(errorThrow, req, res, next)
status.should.have.been.calledWith(400)
})
app.use((err, req, res, next) => {console.log('1')
let errorMessage;
let errorCode;
let errorStatus;
// express validation error 所有傳入參數驗證錯誤
if (err instanceof expressValidation.ValidationError) {
if (err.errors[0].location === 'query' || err.errors[0].location === 'body') {
errorMessage = err.errors[0].messages;
errorCode = 400;
errorStatus = httpStatus.BAD_REQUEST;
}
const error = new APPError.APIError(errorMessage, errorStatus, true, errorCode);
return next(error);
}
return next(err);
});
app.use((err, req, res, next) => {
if (err instanceof expressValidation.ValidationError) {
// validation error contains errors which is an array of error each containing message[]
const unifiedErrorMessage = err.errors.map(error => error.messages.join('. ')).join(' and ');
const error = new APIError(unifiedErrorMessage, err.status, true);
return next(error);
} else if (!(err instanceof APIError)) {
const apiError = new APIError(err.message, err.status, err.isPublic);
return next(apiError);
}
return next(err);
});
app.use((err, req, res, next) => {
if (err instanceof expressValidation.ValidationError) {
// validation error contains errors which is an array of error each containing message[]
const unifiedErrorMessage = err.errors.map(error => error.messages.join('. ')).join(' and ');
const error = new APIError(unifiedErrorMessage, err.status, true);
return next(error);
} else if (!(err instanceof ExtendableError)) {
const apiError = new APIError(err.message, err.status, err.isPublic);
return next(apiError);
}
return next(err);
});
app.use((err, req, res, next) => {
if (err instanceof expressValidation.ValidationError) {
// validation error contains errors which is an array of error each containing message[]
const unifiedErrorMessage = err.errors.map((error) => error.messages.join('. ')).join(' and ');
const error = new APIError(unifiedErrorMessage, err.status, true);
return next(error);
} if (!(err instanceof APIError)) {
const apiError = new APIError(err.message, err.status, err.isPublic);
return next(apiError);
}
return next(err);
});
app.use((err, req, res, next) => {
if (err instanceof expressValidation.ValidationError) {
// validation error contains errors which is an array of error each containing message[]
const unifiedErrorMessage = err.errors.map(error => error.messages.join('. ')).join(' and ');
const error = new APIError(unifiedErrorMessage, err.status, true);
return next(error);
} else if (!(err instanceof APIError)) {
const apiError = new APIError(err.message, err.status, err.isPublic);
return next(apiError);
}
return next(err);
});
app.use((err, req, res, next) => {
if (err instanceof expressValidation.ValidationError) {
// validation error contains errors which is an array of error each containing message[]
const unifiedErrorMessage = err.errors.map(error => error.messages.join('. ')).join(' and ');
const error = new APIError(unifiedErrorMessage, err.status, true);
return next(error);
} else if (!(err instanceof APIError)) {
const apiError = new APIError(err.message, err.status, err.isPublic);
return next(apiError);
}
return next(err);
});
app.use((err, req, res, next) => {
if (err instanceof expressValidation.ValidationError) {
// validation error contains errors which is an array of error each containing message[]
const unifiedErrorMessage = err.errors.map(error => error.messages.join('. ')).join(' and ');
const error = new APIError(unifiedErrorMessage, err.status, true);
return next(error);
} else if (!(err instanceof APIError)) {
const apiError = new APIError(err.message, err.status, err.isPublic);
return next(apiError);
}
return next(err);
});
app.use((err, req, res, next) => {
if (err instanceof expressValidation.ValidationError) {
res.status(err.status).json(err);
} else {
res.status(500)
.json({
status: err.status,
message: err.message
});
}
});
app.use((err, req, res, next) => {
if (err instanceof expressValidation.ValidationError) {
const unifiedErrorMessage = err.errors
.map(error => error.messages.join(". "))
.join(" and ");
return res.status(err.status).json({
message: unifiedErrorMessage
});
}
});