Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}).catch(err => {
// Possible errors: https://www.twilio.com/docs/api/rest/request#get-responses
return this.Promise.reject(new MoleculerError(err.message + " " + err.detail, 500, "SMS_SEND_ERROR"));
});
}
return;
}
/* istanbul ignore next */
if (!err || !(err instanceof Error)) {
res.writeHead(500);
res.end("Internal Server Error");
this.logResponse(req, res);
return;
}
/* istanbul ignore next */
if (!(err instanceof MoleculerError)) {
const e = err;
err = new MoleculerError(e.message, e.code || e.status, e.type, e.data);
err.name = e.name;
}
// Return with the error as JSON object
res.setHeader("Content-type", "application/json; charset=utf-8");
const code = _.isNumber(err.code) && _.inRange(err.code, 400, 599) ? err.code : 500;
res.writeHead(code);
const errObj = _.pick(err, ["name", "message", "code", "type", "data"]);
res.end(JSON.stringify(errObj, null, 2));
this.logResponse(req, res);
},
compose(...mws)(req, res, err => {
if (err) {
/* istanbul ignore next */
if (err instanceof MoleculerError)
return reject(err);
/* istanbul ignore next */
if (err instanceof Error)
return reject(new MoleculerError(err.message, err.code || err.status, err.type)); // TODO err.stack
/* istanbul ignore next */
return reject(new MoleculerError(err));
}
resolve();
});
});
compose(...mws)(req, res, err => {
if (err) {
/* istanbul ignore next */
if (err instanceof MoleculerError)
return reject(err);
/* istanbul ignore next */
if (err instanceof Error)
return reject(new MoleculerError(err.message, err.code || err.status, err.type)); // TODO err.stack
/* istanbul ignore next */
return reject(new MoleculerError(err));
}
resolve();
});
});
let throwMiddleware = jest.fn((req, res, next) => next(new MoleculerError("Some error")));
handler(ctx) {
let a = Number(ctx.params.a);
let b = Number(ctx.params.b);
if (b != 0 && !Number.isNaN(b))
return a / b;
else
throw new MoleculerError("Divide by zero!", 422, ctx.params);
}
}
const authenticate = jest.fn(() => Promise.reject(new MoleculerError("Not available", 400)));
const service = broker.createService(ApiGateway, {
if (!msg.from)
msg.from = this.settings.from;
if (this.transporter) {
this.transporter.sendMail(msg, (err, info) => {
if (err) {
this.logger.warn("Unable to send email: ", err);
reject(new MoleculerRetryableError("Unable to send email! " + err.message));
} else {
this.logger.info("Email message sent.", info.response);
resolve(info);
}
});
}
else
return reject(new MoleculerError("Unable to send email! Invalid mailer transport: " + this.settings.transport));
});
}
handler(ctx) {
if (THROW_ERR && ctx.params.userID == 1)
throw new MoleculerError("Friends is not found!", 404, "FRIENDS_NOT_FOUND", { userID: ctx.params.userID });
return this.Promise.resolve().delay(30).then(() => ctx.params.userID * 3);
}
}
handler(ctx) {
let user = users.find(u => u.username == ctx.params.username && u.password == ctx.params.password);
if (user) {
return this.generateToken(user).then(token => {
return { token };
});
} else
return Promise.reject(new MoleculerError("Invalid credentials", 400));
}
},