further airbrake fix

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2022-02-22 12:13:42 +01:00
parent baea75b11a
commit c1069d2c40
9 changed files with 32 additions and 73 deletions
@@ -13,20 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { getVoidLogger } from '@backstage/backend-common';
import { ConfigReader } from '@backstage/config';
import { extractAirbrakeConfig } from './ExtractAirbrakeConfig';
import * as winston from 'winston';
describe('ExtractAirbrakeConfig', () => {
let oldProcessEnv: NodeJS.ProcessEnv;
let voidLogger: winston.Logger;
beforeEach(() => {
oldProcessEnv = process.env;
voidLogger = getVoidLogger();
});
it('gets the API key', () => {
const config = new ConfigReader({
airbrake: {
@@ -34,34 +25,14 @@ describe('ExtractAirbrakeConfig', () => {
},
});
const airbrakeConfig = extractAirbrakeConfig(config, voidLogger);
const airbrakeConfig = extractAirbrakeConfig(config);
expect(airbrakeConfig.apiKey).toStrictEqual('fakeApiKey');
});
it('does not fail in development', () => {
process.env = {
...oldProcessEnv,
NODE_ENV: 'development',
};
const config = new ConfigReader({});
expect(() => extractAirbrakeConfig(config, voidLogger)).not.toThrow();
const airbrakeConfig = extractAirbrakeConfig(config, voidLogger);
expect(airbrakeConfig.apiKey).toStrictEqual('');
});
it('fails in production', () => {
process.env = {
...oldProcessEnv,
NODE_ENV: 'production',
};
const config = new ConfigReader({});
expect(() => extractAirbrakeConfig(config, voidLogger)).toThrow();
});
afterEach(() => {
process.env = { ...oldProcessEnv };
it('fails for missing keys', () => {
expect(() => extractAirbrakeConfig(new ConfigReader({}))).toThrow();
expect(() =>
extractAirbrakeConfig(new ConfigReader({ airbrake: {} })),
).toThrow();
});
});
@@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Config } from '@backstage/config';
import * as winston from 'winston';
/**
* The configuration needed for the airbrake-backend plugin
@@ -34,27 +34,9 @@ export interface AirbrakeConfig {
* @public
*
* @param config - The config object to extract from
* @param logger - THe logger object
*/
export function extractAirbrakeConfig(
config: Config,
logger: winston.Logger,
): AirbrakeConfig {
try {
return {
apiKey: config.getString('airbrake.apiKey'),
};
} catch (e) {
if (process.env.NODE_ENV !== 'development') {
throw e;
} else {
logger.warn(
'Airbrake config missing, Airbrake plugin will probably not work',
e,
);
return {
apiKey: '',
};
}
}
export function extractAirbrakeConfig(config: Config): AirbrakeConfig {
return {
apiKey: config.getString('airbrake.apiKey'),
};
}
@@ -40,7 +40,7 @@ describe('createRouter', () => {
apiKey: 'fakeApiKey',
},
});
airbrakeConfig = extractAirbrakeConfig(config, voidLogger);
airbrakeConfig = extractAirbrakeConfig(config);
const router = await createRouter({
logger: voidLogger,
@@ -34,7 +34,7 @@ export async function startStandaloneServer(
): Promise<Server> {
const logger = options.logger.child({ service: 'airbrake-backend' });
const config = await loadBackendConfig({ logger, argv: process.argv });
const airbrakeConfig = extractAirbrakeConfig(config, logger);
const airbrakeConfig = extractAirbrakeConfig(config);
logger.debug('Starting application server...');
const router = await createRouter({
logger,