Skip to content

Commit 9125c0a

Browse files
committed
Improve Error Handling, Cert Bug Hack/Patch
1 parent 3730b7d commit 9125c0a

File tree

6 files changed

+66
-52
lines changed

6 files changed

+66
-52
lines changed

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@
140140
}
141141
]
142142
},
143+
"configuration":{
144+
"type": "object",
145+
"title": "Krinql Configuration",
146+
"properties": {
147+
"krinql-vscode.httpSystemCertificatesHotFix": {
148+
"type": "boolean",
149+
"default": null
150+
}
151+
}
152+
},
143153
"menus": {
144154
"view/title": [
145155
{

src/functions/ask.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AxiosInstance } from 'axios';
22
import * as vscode from 'vscode';
3-
import { LOGIN_URL } from '../config';
3+
import { handleNoResponse } from '../util/errorHandlers';
44

55
export async function askQuestion(httpHandler: AxiosInstance) {
66
const question = await vscode.window.showInputBox(
@@ -37,21 +37,11 @@ export async function askQuestion(httpHandler: AxiosInstance) {
3737
} catch (err: any) {
3838
console.log({err});
3939
if (!err.response) {
40-
console.log({err});
41-
if(err.name === "auth/no-token"){
42-
vscode.window.showWarningMessage("Please login to your Krinql account.", 'Login')
43-
.then(selection => {
44-
if (selection === 'Login') {
45-
vscode.env.openExternal(vscode.Uri.parse(LOGIN_URL));
46-
}
47-
});
48-
} else {
49-
vscode.window.showErrorMessage(`Error Contacting API ${err?.message}`);
50-
}
40+
await handleNoResponse(err);
5141
reject("Error Contacting API");
5242
}else{
53-
vscode.window.showErrorMessage(err.response.data.message);
54-
reject(err.response.data.message);
43+
vscode.window.showErrorMessage(err?.response?.data?.message || err?.code);
44+
reject(err?.response?.data?.message || err?.code);
5545
}
5646
}
5747
}

src/functions/docstring.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { AxiosInstance } from 'axios';
22
import * as vscode from 'vscode';
33
import { getInsert, getSelectedText } from '../util/util';
44
import { Commentifier } from '../util/comments';
5-
import { LOGIN_URL } from '../config';
5+
import { handleNoResponse } from '../util/errorHandlers';
66

77
let commentify = new Commentifier().commentify;
88

@@ -34,20 +34,11 @@ export async function generateDocstring(httpHandler: AxiosInstance) {
3434
} catch (err: any) {
3535
console.log({err});
3636
if (!err.response) {
37-
if(err.name === "auth/no-token"){
38-
vscode.window.showWarningMessage("Please login to your Krinql account.", 'Login')
39-
.then(selection => {
40-
if (selection === 'Login') {
41-
vscode.env.openExternal(vscode.Uri.parse(LOGIN_URL));
42-
}
43-
});
44-
} else {
45-
vscode.window.showErrorMessage(`Error Contacting API ${err?.message}`);
46-
}
37+
await handleNoResponse(err);
4738
reject("Error Contacting API");
4839
}else{
49-
vscode.window.showErrorMessage(err.response.data.message);
50-
reject(err.response.data.message);
40+
vscode.window.showErrorMessage(err?.response?.data?.message || err?.code);
41+
reject(err?.response?.data?.message || err?.code);
5142
}
5243
}
5344
} else {

src/functions/explain.ts

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { AxiosInstance } from 'axios';
22
import * as vscode from 'vscode';
33
import { getInsert, getSelectedText } from '../util/util';
44
import { Commentifier } from '../util/comments';
5-
import { LOGIN_URL } from '../config';
5+
import { handleNoResponse } from '../util/errorHandlers';
66

77
let commentify = new Commentifier().commentify;
88

@@ -31,20 +31,11 @@ export async function explainDocument(httpHandler: AxiosInstance) {
3131
} catch (err: any) {
3232
console.log({err});
3333
if(!err.response) {
34-
if(err.name === "auth/no-token"){
35-
vscode.window.showWarningMessage("Please login to your Krinql account.", 'Login')
36-
.then(selection => {
37-
if (selection === 'Login') {
38-
vscode.env.openExternal(vscode.Uri.parse(LOGIN_URL));
39-
}
40-
});
41-
} else {
42-
vscode.window.showErrorMessage(`Error Contacting API ${err?.message}`);
43-
}
34+
await handleNoResponse(err);
4435
reject("Error Contacting API");
4536
}else{
46-
vscode.window.showErrorMessage(err.response.data.message);
47-
reject(err.response.data.message);
37+
vscode.window.showErrorMessage(err?.response?.data?.message || err?.code);
38+
reject(err?.response?.data?.message || err?.code);
4839
}
4940
}
5041
} else {
@@ -79,20 +70,11 @@ export async function explainCode(httpHandler: AxiosInstance) {
7970
} catch (err: any) {
8071
console.log({err});
8172
if (!err.response) {
82-
if(err.name === "auth/no-token"){
83-
vscode.window.showWarningMessage("Please login to your Krinql account.", 'Login')
84-
.then(selection => {
85-
if (selection === 'Login') {
86-
vscode.env.openExternal(vscode.Uri.parse(LOGIN_URL));
87-
}
88-
});
89-
} else {
90-
vscode.window.showErrorMessage(`Error Contacting API ${err?.message}`);
91-
}
73+
await handleNoResponse(err);
9274
reject("Error Contacting API");
9375
} else {
94-
vscode.window.showErrorMessage(err.response.data.message);
95-
reject(err.response.data.message);
76+
vscode.window.showErrorMessage(err?.response?.data?.message || err?.code);
77+
reject(err?.response?.data?.message || err?.code);
9678
}
9779
}
9880
} else {

src/util/certErrorHotfix.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { workspace, ConfigurationTarget } from 'vscode';
2+
import * as vscode from 'vscode';
3+
4+
export async function certConfigHack(): Promise<void> {
5+
// See https://github.com/krinql/vscode/issues/1
6+
// and https://github.com/microsoft/vscode/issues/136787
7+
if (workspace.getConfiguration('http').get<boolean>('systemCertificates')) {
8+
// Set a sentinel so that we know that we set it rather than the user, and thus we can revert it later.
9+
await workspace.getConfiguration('krinql-vscode').update('httpSystemCertificatesHotFix', true, ConfigurationTarget.Global);
10+
await workspace.getConfiguration('http').update('systemCertificates', false, ConfigurationTarget.Global);
11+
// Setting docs say that a window reload is required for it to take effect.
12+
vscode.window.showInformationMessage('Attempting to apply patch for SSL Error, Window will reload');
13+
await vscode.commands.executeCommand('workbench.action.reloadWindow');
14+
}
15+
// Already tried applying the hack and it didn't work, so give up.
16+
}

src/util/errorHandlers.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as vscode from 'vscode';
2+
import { LOGIN_URL } from '../config';
3+
import {certConfigHack} from './certErrorHotfix';
4+
export async function handleNoResponse(err: any): Promise<void> {
5+
if (!err.response) {
6+
if(err.name === "auth/no-token"){
7+
vscode.window.showWarningMessage("Please login to your Krinql account.", 'Login')
8+
.then(selection => {
9+
if (selection === 'Login') {
10+
vscode.env.openExternal(vscode.Uri.parse(LOGIN_URL));
11+
}
12+
});
13+
}
14+
else if(err?.message?.includes("certificate has expired")){
15+
console.log("Attempting to fix certificate error");
16+
// Bugfix Patch/Hack for electron certificate error
17+
// See https://github.com/krinql/vscode/issues/1
18+
// and https://github.com/microsoft/vscode/issues/136787
19+
await certConfigHack();
20+
}
21+
else {
22+
vscode.window.showErrorMessage(`Error Contacting API ${err?.message}`);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)