From 91c7b0c6f5f62e451a83bcdee63cdbedae59c9ad Mon Sep 17 00:00:00 2001 From: Bsian Date: Wed, 1 Jan 2020 22:21:41 +0000 Subject: [PATCH] Error catching for invalid certificates --- src/commands/parse.ts | 9 +++++++-- src/commands/parseall.ts | 1 + src/functions/index.ts | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/commands/parse.ts b/src/commands/parse.ts index 5c1abf3..2bd957b 100644 --- a/src/commands/parse.ts +++ b/src/commands/parse.ts @@ -3,7 +3,7 @@ import { parseCert } from '@ghaiklor/x509'; import { Message } from 'eris'; import { Client } from '..'; import { Command, RichEmbed } from '../class'; -import { parseCertificate } from '../functions'; +import { parseCertificate, Certificate } from '../functions'; export default class Parse extends Command { constructor(client: Client) { @@ -26,7 +26,12 @@ export default class Parse extends Command { return message.channel.createMessage(`***${this.client.stores.emojis.error} Cannot locate Validation directory.***`); } if (!dir.length) return message.channel.createMessage(`***${this.client.stores.emojis.error} Cannot locate certificate.***`); - const cert = await parseCertificate(this.client, `${account.homepath}/Validation/${dir[0]}`); + let cert: Certificate; + try { + cert = await parseCertificate(this.client, `${account.homepath}/Validation/${dir[0]}`); + } catch (error) { + if (error.message.includes('panic: Certificate PEM Encode == nil')) return message.channel.createMessage(`***${this.client.stores.emojis.error} Invalid certificate.***`); + } // const cert = parseCert(`${account.homepath}/Validation/${dir[0]}`); const subjectCommonName = cert.subject.commonName || 'Not Specified'; const subjectEmailAddress = cert.subject.emailAddress || 'Not Specified'; diff --git a/src/commands/parseall.ts b/src/commands/parseall.ts index 999365b..b2a1cf0 100644 --- a/src/commands/parseall.ts +++ b/src/commands/parseall.ts @@ -48,6 +48,7 @@ export default class Parseall extends Command { else final.push(`${this.client.stores.emojis.success} **${a.username}** Expires in ${time}`); } catch (error) { if (error.message.includes('no such file or directory') || error.message.includes('File doesn\'t exist.')) final.push(`${this.client.stores.emojis.error} **${a.username}** Unable to locate certificate`); + if (error.message.includes('panic: Certificate PEM Encode == nil')) final.push(`${this.client.stores.emojis.error} ** ${a.username}** Invalid certificate`); else throw error; } }); diff --git a/src/functions/index.ts b/src/functions/index.ts index 4395294..bf1b87c 100644 --- a/src/functions/index.ts +++ b/src/functions/index.ts @@ -1,4 +1,4 @@ export { default as checkLock } from './checkLock'; export { default as dataConversion } from './dataConversion'; export { default as checkSS } from './checkSS'; -export { default as parseCertificate } from './parseCertificate'; +export { default as parseCertificate, Certificate } from './parseCertificate';