Skip to content

Commit ef7958f

Browse files
committed
feat: improved error handling, context menu items
1 parent 7285524 commit ef7958f

File tree

5 files changed

+56
-16
lines changed

5 files changed

+56
-16
lines changed

package.json

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,37 +86,32 @@
8686
"keybindings": [
8787
{
8888
"command": "krinql-vscode.explain",
89-
"key": "cmd+ctrl+e",
9089
"mac": "cmd+ctrl+e",
91-
"win": "ctrl+alt+e",
90+
"key": "ctrl+alt+e",
9291
"when": "editorHasSelection"
9392
},
9493
{
9594
"command": "krinql-vscode.explaindocument",
96-
"key": "cmd+ctrl+e",
9795
"mac": "cmd+ctrl+e",
98-
"win": "ctrl+alt+e",
96+
"key": "ctrl+alt+e",
9997
"when": "!editorHasSelection"
10098
},
10199
{
102100
"command": "krinql-vscode.ask",
103-
"key": "cmd+ctrl+a",
104101
"mac": "cmd+ctrl+a",
105-
"win": "ctrl+alt+a",
102+
"key": "ctrl+alt+a",
106103
"when": "editorHasSelection"
107104
},
108105
{
109106
"command": "krinql-vscode.ask",
110-
"key": "cmd+ctrl+a",
111107
"mac": "cmd+ctrl+a",
112-
"win": "ctrl+alt+a",
108+
"key": "ctrl+alt+a",
113109
"when": "!editorHasSelection"
114110
},
115111
{
116112
"command": "krinql-vscode.docstring",
117-
"key": "cmd+ctrl+d",
118113
"mac": "cmd+ctrl+d",
119-
"win": "ctrl+alt+d",
114+
"key": "ctrl+alt+d",
120115
"when": "editorHasSelection"
121116
}
122117
],
@@ -157,23 +152,23 @@
157152
],
158153
"editor/context": [
159154
{
160-
"when": "editorHasSelection && isAuthed",
155+
"when": "editorHasSelection",
161156
"command": "krinql-vscode.explain"
162157
},
163158
{
164-
"when": "!editorHasSelection && isAuthed",
159+
"when": "!editorHasSelection",
165160
"command": "krinql-vscode.explaindocument"
166161
},
167162
{
168-
"when": "editorHasSelection && isAuthed",
163+
"when": "editorHasSelection",
169164
"command": "krinql-vscode.ask"
170165
},
171166
{
172-
"when": "!editorHasSelection && isAuthed",
167+
"when": "!editorHasSelection",
173168
"command": "krinql-vscode.ask"
174169
},
175170
{
176-
"when": "editorHasSelection && isAuthed",
171+
"when": "editorHasSelection",
177172
"command": "krinql-vscode.docstring"
178173
}
179174
]

src/extension.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,15 @@ export function activate(context: vscode.ExtensionContext) {
4343

4444
httpHandler.interceptors.request.use(
4545
async (config) => {
46+
const token = storageManager.get('token', null);
47+
if(!token){
48+
const customError = new Error('Please Sign in');
49+
customError.name = "auth/no-token";
50+
throw customError;
51+
};
4652
config.headers = config.baseURL!=="https://securetoken.googleapis.com/v1" ? {
4753
// eslint-disable-next-line @typescript-eslint/naming-convention
48-
'Authorization': `Bearer ${storageManager.get('token', null)}`,
54+
'Authorization': `Bearer ${token}`,
4955
// eslint-disable-next-line @typescript-eslint/naming-convention
5056
'Content-Type': 'application/json'
5157
}:{

src/functions/ask.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { AxiosInstance } from 'axios';
22
import * as vscode from 'vscode';
3+
import { LOGIN_URL } from '../config';
34

45
export async function askQuestion(httpHandler: AxiosInstance) {
56
const question = await vscode.window.showInputBox(
@@ -37,7 +38,16 @@ export async function askQuestion(httpHandler: AxiosInstance) {
3738
console.log({err});
3839
if (!err.response) {
3940
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 {
4049
vscode.window.showErrorMessage(`Error Contacting API ${err?.message}`);
50+
}
4151
reject("Error Contacting API");
4252
}else{
4353
vscode.window.showErrorMessage(err.response.data.message);

src/functions/docstring.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +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';
56

67
let commentify = new Commentifier().commentify;
78

@@ -33,7 +34,16 @@ export async function generateDocstring(httpHandler: AxiosInstance) {
3334
} catch (err: any) {
3435
console.log({err});
3536
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 {
3645
vscode.window.showErrorMessage(`Error Contacting API ${err?.message}`);
46+
}
3747
reject("Error Contacting API");
3848
}else{
3949
vscode.window.showErrorMessage(err.response.data.message);

src/functions/explain.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +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';
56

67
let commentify = new Commentifier().commentify;
78

@@ -30,7 +31,16 @@ export async function explainDocument(httpHandler: AxiosInstance) {
3031
} catch (err: any) {
3132
console.log({err});
3233
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 {
3342
vscode.window.showErrorMessage(`Error Contacting API ${err?.message}`);
43+
}
3444
reject("Error Contacting API");
3545
}else{
3646
vscode.window.showErrorMessage(err.response.data.message);
@@ -69,7 +79,16 @@ export async function explainCode(httpHandler: AxiosInstance) {
6979
} catch (err: any) {
7080
console.log({err});
7181
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 {
7290
vscode.window.showErrorMessage(`Error Contacting API ${err?.message}`);
91+
}
7392
reject("Error Contacting API");
7493
} else {
7594
vscode.window.showErrorMessage(err.response.data.message);

0 commit comments

Comments
 (0)