Skip to content

Commit

Permalink
fix: #1
Browse files Browse the repository at this point in the history
  • Loading branch information
darshkpatel committed Dec 5, 2021
1 parent 3730b7d commit 54e63bd
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 44 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@
}
]
},
"configuration":{
"krinql.httpSystemCertificatesHotFix": {
"type": "boolean",
"default": null
}
},
"menus": {
"view/title": [
{
Expand Down
14 changes: 2 additions & 12 deletions src/functions/ask.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AxiosInstance } from 'axios';
import * as vscode from 'vscode';
import { LOGIN_URL } from '../config';
import { handleNoResponse } from '../util/errorHandlers';

export async function askQuestion(httpHandler: AxiosInstance) {
const question = await vscode.window.showInputBox(
Expand Down Expand Up @@ -37,17 +37,7 @@ export async function askQuestion(httpHandler: AxiosInstance) {
} catch (err: any) {
console.log({err});
if (!err.response) {
console.log({err});
if(err.name === "auth/no-token"){
vscode.window.showWarningMessage("Please login to your Krinql account.", 'Login')
.then(selection => {
if (selection === 'Login') {
vscode.env.openExternal(vscode.Uri.parse(LOGIN_URL));
}
});
} else {
vscode.window.showErrorMessage(`Error Contacting API ${err?.message}`);
}
await handleNoResponse(err);
reject("Error Contacting API");
}else{
vscode.window.showErrorMessage(err.response.data.message);
Expand Down
13 changes: 2 additions & 11 deletions src/functions/docstring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AxiosInstance } from 'axios';
import * as vscode from 'vscode';
import { getInsert, getSelectedText } from '../util/util';
import { Commentifier } from '../util/comments';
import { LOGIN_URL } from '../config';
import { handleNoResponse } from '../util/errorHandlers';

let commentify = new Commentifier().commentify;

Expand Down Expand Up @@ -34,16 +34,7 @@ export async function generateDocstring(httpHandler: AxiosInstance) {
} catch (err: any) {
console.log({err});
if (!err.response) {
if(err.name === "auth/no-token"){
vscode.window.showWarningMessage("Please login to your Krinql account.", 'Login')
.then(selection => {
if (selection === 'Login') {
vscode.env.openExternal(vscode.Uri.parse(LOGIN_URL));
}
});
} else {
vscode.window.showErrorMessage(`Error Contacting API ${err?.message}`);
}
await handleNoResponse(err);
reject("Error Contacting API");
}else{
vscode.window.showErrorMessage(err.response.data.message);
Expand Down
24 changes: 3 additions & 21 deletions src/functions/explain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AxiosInstance } from 'axios';
import * as vscode from 'vscode';
import { getInsert, getSelectedText } from '../util/util';
import { Commentifier } from '../util/comments';
import { LOGIN_URL } from '../config';
import { handleNoResponse } from '../util/errorHandlers';

let commentify = new Commentifier().commentify;

Expand Down Expand Up @@ -31,16 +31,7 @@ export async function explainDocument(httpHandler: AxiosInstance) {
} catch (err: any) {
console.log({err});
if(!err.response) {
if(err.name === "auth/no-token"){
vscode.window.showWarningMessage("Please login to your Krinql account.", 'Login')
.then(selection => {
if (selection === 'Login') {
vscode.env.openExternal(vscode.Uri.parse(LOGIN_URL));
}
});
} else {
vscode.window.showErrorMessage(`Error Contacting API ${err?.message}`);
}
await handleNoResponse(err);
reject("Error Contacting API");
}else{
vscode.window.showErrorMessage(err.response.data.message);
Expand Down Expand Up @@ -79,16 +70,7 @@ export async function explainCode(httpHandler: AxiosInstance) {
} catch (err: any) {
console.log({err});
if (!err.response) {
if(err.name === "auth/no-token"){
vscode.window.showWarningMessage("Please login to your Krinql account.", 'Login')
.then(selection => {
if (selection === 'Login') {
vscode.env.openExternal(vscode.Uri.parse(LOGIN_URL));
}
});
} else {
vscode.window.showErrorMessage(`Error Contacting API ${err?.message}`);
}
await handleNoResponse(err);
reject("Error Contacting API");
} else {
vscode.window.showErrorMessage(err.response.data.message);
Expand Down
16 changes: 16 additions & 0 deletions src/util/certErrorHotfix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { workspace, ConfigurationTarget } from 'vscode';
import * as vscode from 'vscode';

export async function certConfigHack(): Promise<void> {
// See https://github.com/krinql/vscode/issues/1
// and https://github.com/microsoft/vscode/issues/136787
if (workspace.getConfiguration('http').get<boolean>('systemCertificates')) {
// Set a sentinel so that we know that we set it rather than the user, and thus we can revert it later.
await workspace.getConfiguration('krinql').update('httpSystemCertificatesHotFix', true, ConfigurationTarget.Global);
await workspace.getConfiguration('http').update('systemCertificates', false, ConfigurationTarget.Global);
// Setting docs say that a window reload is required for it to take effect.
vscode.window.showInformationMessage('Attempting to apply patch for SSL Error, Window will reload');
await vscode.commands.executeCommand('workbench.action.reloadWindow');
}
// Already tried applying the hack and it didn't work, so give up.
}
24 changes: 24 additions & 0 deletions src/util/errorHandlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as vscode from 'vscode';
import { LOGIN_URL } from '../config';
import {certConfigHack} from './certErrorHotfix';
export async function handleNoResponse(err: any): Promise<void> {
if (!err.response) {
if(err.name === "auth/no-token"){
vscode.window.showWarningMessage("Please login to your Krinql account.", 'Login')
.then(selection => {
if (selection === 'Login') {
vscode.env.openExternal(vscode.Uri.parse(LOGIN_URL));
}
});
}
else if(err?.message?.includes("certificate has expired")){
// Bugfix Patch/Hack for electron certificate error
// See https://github.com/krinql/vscode/issues/1
// and https://github.com/microsoft/vscode/issues/136787
await certConfigHack();
}
else {
vscode.window.showErrorMessage(`Error Contacting API ${err?.message}`);
}
}
}

0 comments on commit 54e63bd

Please sign in to comment.