Skip to content

Commit

Permalink
add fun exception error processor (#712)
Browse files Browse the repository at this point in the history
add fun exception processor

1. add Docker not started or not installed error processor
2. add fc service not enabled error processor
3. add ram inactive error processor
  • Loading branch information
tanhe123 authored Jan 3, 2020
1 parent f83ed82 commit 61fb948
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/appenders/error-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { FilterChain } = require('../error-processor');

// https://github.com/log4js-node/log4js-node/blob/master/docs/writing-appenders.md
function errorProcessorAppender(layout, timezoneOffset) {
const filterChain = new FilterChain({});
const filterChain = new FilterChain();
return (loggingEvent) => {
const message = layout(loggingEvent, timezoneOffset) + '\n';
filterChain.process(message);
Expand Down
1 change: 0 additions & 1 deletion lib/commands/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ async function getModifiedTimes(tplPath) {
}

async function deploy(context) {

let tplPath = context.template;

if (!tplPath) {
Expand Down
65 changes: 57 additions & 8 deletions lib/error-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@ const unrefTimeout = require('./unref-timeout');
const detectMocha = require('detect-mocha');

class FilterChain {
constructor(options) {
constructor(options = {}) {
this.processors = [
new PuppeteerInvalidPlatformProcessor(options),
new DynamicLinkLibraryMissingProcessor(options),
new NoSpaceLeftOnDeviceProcessor(options),
new MissingAptGetProcessor(options)];
new MissingAptGetProcessor(options),
new DockerNotStartedOrInstalledErrorProcessor(options),
new FcServiceNotEnabledProcessor(options),
new RamInactiveErrorProcessor(options)];
}

async process(message) {
async process(message, err) {
for (const processor of this.processors) {
if (processor.match(message)) {
await processor.process(message);
if (!message) { message = ''; }

if (processor.match(message, err)) {
await processor.process(message, err);
await processor.postProcess();
return;
return true;
}
}
}
Expand All @@ -34,8 +39,8 @@ class ErrorProcessor {
this.functionName = options.functionName;
}

match(message) { }
async process(message) { }
match(message, err) { }
async process(message, err) { }

_autoExist() {
process.nextTick(() => {
Expand All @@ -50,7 +55,51 @@ class ErrorProcessor {
}

async postProcess() {
console.log();
}
}

class DockerNotStartedOrInstalledErrorProcessor extends ErrorProcessor {
match(message, err) {
if (_.includes(message, 'connect ECONNREFUSED /var/run/docker.sock')
|| _.includes(message, 'Error: connect ENOENT //./pipe/docker_engine')) {
return true;
}

return false;
}

async process(message) {
console.log(red('Fun detected that Docker is not installed on your host or not started. Please run \'docker ps\' command to check docker status.'));
}
}

class FcServiceNotEnabledProcessor extends ErrorProcessor {
match(message, err) {
if (_.includes(message, 'FC service is not enabled for current user')) {
return true;
}

return false;
}

async process(message) {
console.log(red('FC service is not enabled for current user. Please enable FC service before using fun.\nYou can enable FC service on this page https://www.aliyun.com/product/fc .'));
}
}

class RamInactiveErrorProcessor extends ErrorProcessor {
match(message, err) {
if (_.includes(message, 'Account is inactive to this service')
&& _.includes(message, 'ram.aliyuncs.com')) {
return true;
}

return false;
}

async process(message) {
console.log(red('Ram service is not enabled for current user. Please enable Ram service before using fun.\nYou can enable Ram service on this page https://www.aliyun.com/product/ram .'));
}
}

Expand Down
31 changes: 18 additions & 13 deletions lib/exception-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@
const debug = require('debug');
const unrefTimeout = require('./unref-timeout');
const { red } = require('colors');
const { FilterChain } = require('./error-processor');

const filterChain = new FilterChain();

const handler = function (err) {
// If the verbose option is true, in addition to the message,
// print the stack of the error.
if (debug.enabled('*') && err.stack) {
console.error(err.stack);
} else if (err.message) {
console.error(red(err.message));
} else {
console.error(err);
}
filterChain.process(err.message, err).then(processed => {
// If the verbose option is true, in addition to the message,
// print the stack of the error.
if (debug.enabled('*') && err.stack) {
console.error(err.stack);
} else if (err.message) {
console.error(red(err.message));
} else {
console.error(err);
}

// in order visitor request has been sent out

// in order visitor request has been sent out

unrefTimeout(() => {
process.exit(-1); // eslint-disable-line
unrefTimeout(() => {
process.exit(-1); // eslint-disable-line
});
});
};

Expand Down

0 comments on commit 61fb948

Please sign in to comment.