How to use the moleculer.MoleculerError function in moleculer

To help you get started, we’ve selected a few moleculer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github moleculerjs / moleculer-addons / packages / moleculer-twilio / src / index.js View on Github external
}).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"));
			});
		}
github moleculerjs / moleculer-web / src / index.js View on Github external
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);
		},
github moleculerjs / moleculer-web / src / utils.js View on Github external
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();
		});
	});
github moleculerjs / moleculer-web / src / utils.js View on Github external
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();
		});
	});
github moleculerjs / moleculer-web / test / integration / index.spec.js View on Github external
	let throwMiddleware = jest.fn((req, res, next) => next(new MoleculerError("Some error")));
github moleculerjs / moleculer-web / test / services / math.service.js View on Github external
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);
			}
		}
github moleculerjs / moleculer-web / test / integration / index.spec.js View on Github external
		const authenticate = jest.fn(() => Promise.reject(new MoleculerError("Not available", 400)));
		const service = broker.createService(ApiGateway, {
github moleculerjs / moleculer-addons / packages / moleculer-mail / src / index.js View on Github external
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));

			});
		}
github moleculerjs / moleculer-metrics / packages / moleculer-jaeger / examples / simple / index.js View on Github external
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);
			}
		}
github moleculerjs / moleculer-web / examples / auth.service.js View on Github external
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));
			}
		},