Skip to content

Commit

Permalink
making default server level error handler throw
Browse files Browse the repository at this point in the history
  • Loading branch information
iangeckeler committed Aug 14, 2019
1 parent d7ef321 commit 70623dc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
14 changes: 8 additions & 6 deletions __tests__/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ xdescribe("Unit tests for Server", () => {
server.bind({ port: "0.0.0.0:3000" });
}).toThrow();
});
});

describe("Tests for addService", () => {
it("addService composes and adds an async function.", () => {
const server = new Server();
server.addService(testService, {
Expand Down Expand Up @@ -80,17 +82,17 @@ xdescribe("Unit tests for Server", () => {
});
});

describe("Unit tests for bind.", () => {
xdescribe("Unit tests for bind.", () => {
it("Bind should support a single port insecurely if no config supplied.", () => {
const server = new Server();
const boundPorts = server.bind("0.0.0.0:3000");
expect(boundPorts.length).toBe(1);
expect(server._ports.length).toBe(1);
});

it("If no cert/key is passed with an array of ports, they are all generated but insecure.", () => {
const server = new Server();
const boundPorts = server.bind(["0.0.0.0:3000", "0.0.0.0:3001"]);
expect(boundPorts.length).toBe(2);
expect(server._ports.length).toBe(2);
});

it("Properly binds one SSL", () => {
Expand All @@ -101,7 +103,7 @@ describe("Unit tests for bind.", () => {
privateKey: keyPath,
certificate: certPath
});
expect(boundPorts.length).toBe(1);
expect(server._ports.length).toBe(1);
});

it("If array of ports and certs/keys are passed each port at index in ports array is bound matching the cert at the same index of the certs array", () => {
Expand All @@ -118,7 +120,7 @@ describe("Unit tests for bind.", () => {
null
]
);
expect(boundPorts.length).toBe(2);
expect(server._ports.length).toBe(2);
});

it("If one cert/key pair is passed, it is applied to all of the different ports.", () => {
Expand All @@ -129,7 +131,7 @@ describe("Unit tests for bind.", () => {
privateKey: keyPath,
certificate: certPath
});
expect(boundPorts.length).toBe(2);
expect(server._ports.length).toBe(2);
});
});

Expand Down
7 changes: 4 additions & 3 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = class Server extends grpc.Server {
check: checkHandler.bind(this),
watch: watchHandler.bind(this)
});
this._ports = [];
}
/**
* Connects handlers and middleware functionality to your gRPC methods * as defined in your `.proto` file.
Expand Down Expand Up @@ -96,7 +97,6 @@ server.addService(
// console.log(service);
// add to the status map
this._statusMap[serviceDefinition._serviceName] = "SERVING";
console.log(this._statusMap);

// handlerObject ~= { unaryChat: [waitFor, unaryChat], serverStream,
// clientStream, bidiChat }
Expand Down Expand Up @@ -133,6 +133,7 @@ server.addService(
}
// console.log('handlers:', handlers);
super.addService(service, handlers);
return this;
// console.log(this instanceof grpc.Server)
}

Expand Down Expand Up @@ -197,7 +198,6 @@ server.bind( '0.0.0.0:3000', {certificate, privateKey } )
// going to assume its not an object and is in fact an array
config.forEach(e => configs.push(e));
}
console.log(ports, configs);
if (ports.length !== configs.length && configs.length !== 1) {
throw new Error(
"Ports and configs lengths should match, unless you want the same certificate and key to encrypt all ports. In that case, ports is an array, and supply a single config."
Expand Down Expand Up @@ -256,7 +256,8 @@ server.bind( '0.0.0.0:3000', {certificate, privateKey } )
);
}
}
return boundPorts;
this._ports = this._ports.concat(boundPorts);
return this;
}

/**
Expand Down
15 changes: 10 additions & 5 deletions lib/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,15 @@ module.exports = function compose(
if (uncaughtErrorHandler) {
uncaughtErrorHandler(err, unaryCall);
} else {
console.log(
"Error inside of call, to handle uncaught errors, input an (err, call) callback into the fourth parameter of addService method.\n\n",
// console.log(
// "Uncaught error inside of call, to handle uncaught errors with a custom error handler, input an (err, call) callback into the fourth parameter of addService method.\n\n",
// "Error:\n\n",
// err,
// "Call:\n\n",
// call
// );
throw new Error(
"Uncaught error inside of call, to handle uncaught errors with a custom error handler, input an (err, call) callback into the fourth parameter of addService method.\n\n",
"Error:\n\n",
err,
"Call:\n\n",
Expand All @@ -110,9 +117,7 @@ module.exports = function compose(
}
}
};
// function() {
// throw new Error("unhandled");
// };

default:
return;
}
Expand Down

0 comments on commit 70623dc

Please sign in to comment.