瀏覽代碼

Adds basic error handling

main
Eric Amodio 1 年之前
committed by Keith Daulton
父節點
當前提交
c39e7e7c38
共有 1 個檔案被更改,包括 74 行新增37 行删除
  1. +74
    -37
      src/plus/focus/focusService.ts

+ 74
- 37
src/plus/focus/focusService.ts 查看文件

@ -1,6 +1,8 @@
import type { Disposable } from 'vscode';
import { Uri } from 'vscode';
import type { Container } from '../../container';
import { log } from '../../system/decorators/log';
import { Logger } from '../../system/logger';
import { getLogScope } from '../../system/logger.scope';
import type { ServerConnection } from '../gk/serverConnection';
export interface FocusItem {
@ -69,74 +71,109 @@ export class FocusService implements Disposable {
dispose(): void {}
private async delete(id: string): Promise<void> {
const rsp = await this.connection.fetch(
Uri.joinPath(this.connection.baseGkApiUri, `v1/enrich-items/${id}`).toString(),
{
method: 'DELETE',
},
);
if (!rsp.ok) {
private async delete(id: string, context: 'unpin' | 'unsnooze'): Promise<void> {
const scope = getLogScope();
try {
const rsp = await this.connection.fetchGkDevApi(`v1/enrich-items/${id}`, { method: 'DELETE' });
if (!rsp.ok) throw new Error(`Unable to ${context} item '${id}': (${rsp.status}) ${rsp.statusText}`);
} catch (ex) {
Logger.error(ex, scope);
debugger;
throw new Error(`Unable to delete enrichment: ${rsp.statusText}`);
throw ex;
}
}
@log()
async get(type?: EnrichedItemResponse['type']): Promise<EnrichedItem[]> {
type Result = { data: EnrichedItemResponse[] };
const scope = getLogScope();
try {
type Result = { data: EnrichedItemResponse[] };
const rsp = await this.connection.fetch(
Uri.joinPath(this.connection.baseGkApiUri, 'v1/enrich-items').toString(),
{
method: 'GET',
},
);
const rsp = await this.connection.fetchGkDevApi('v1/enrich-items', { method: 'GET' });
const result = (await rsp.json()) as Result;
return type == null ? result.data : result.data.filter(i => i.type === type);
const result = (await rsp.json()) as Result;
return type == null ? result.data : result.data.filter(i => i.type === type);
} catch (ex) {
Logger.error(ex, scope);
debugger;
throw ex;
}
}
@log()
getPins(): Promise<EnrichedItem[]> {
return this.get('pin');
}
@log()
getSnoozed(): Promise<EnrichedItem[]> {
return this.get('snooze');
}
@log()
async pinItem(item: FocusItem): Promise<EnrichedItem> {
type Result = { data: EnrichedItemResponse };
const scope = getLogScope();
try {
type Result = { data: EnrichedItemResponse };
const rsp = await this.connection.fetch(
Uri.joinPath(this.connection.baseGkApiUri, 'v1/enrich-items/pin').toString(),
{
const rsp = await this.connection.fetchGkDevApi('v1/enrich-items/pin', {
method: 'POST',
body: JSON.stringify(item satisfies EnrichedItemRequest),
},
);
const result = (await rsp.json()) as Result;
return result.data;
});
if (!rsp.ok) {
throw new Error(
`Unable to pin item '${item.provider}|${item.repositoryOwner}/${item.repositoryName}#${item.id}': (${rsp.status}) ${rsp.statusText}`,
);
}
const result = (await rsp.json()) as Result;
return result.data;
} catch (ex) {
Logger.error(ex, scope);
debugger;
throw ex;
}
}
@log()
unpinItem(id: string): Promise<void> {
return this.delete(id);
return this.delete(id, 'unpin');
}
@log()
async snoozeItem(item: FocusItem): Promise<EnrichedItem> {
type Result = { data: EnrichedItemResponse };
const scope = getLogScope();
try {
type Result = { data: EnrichedItemResponse };
const rsp = await this.connection.fetch(
Uri.joinPath(this.connection.baseGkApiUri, 'v1/enrich-items/snooze').toString(),
{
const rsp = await this.connection.fetchGkDevApi('v1/enrich-items/snooze', {
method: 'POST',
body: JSON.stringify(item satisfies EnrichedItemRequest),
},
);
const result = (await rsp.json()) as Result;
return result.data;
});
if (!rsp.ok) {
throw new Error(
`Unable to snooze item '${item.provider}|${item.repositoryOwner}/${item.repositoryName}#${item.id}': (${rsp.status}) ${rsp.statusText}`,
);
}
const result = (await rsp.json()) as Result;
return result.data;
} catch (ex) {
Logger.error(ex, scope);
debugger;
throw ex;
}
}
@log()
unsnoozeItem(id: string): Promise<void> {
return this.delete(id);
return this.delete(id, 'unsnooze');
}
}

Loading…
取消
儲存