cleanup of new result code
This commit is contained in:
parent
498e2611ae
commit
5a032a87ae
23 changed files with 323 additions and 303 deletions
|
|
@ -1,14 +1,14 @@
|
|||
import type { APIModel } from 'packages/obsidian/src/api/APIModel';
|
||||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind, toAppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { Logger } from 'packages/obsidian/src/utils/Logger';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind, toMdbError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import type { Result } from 'packages/obsidian/src/utils/result';
|
||||
import { err, ok } from 'packages/obsidian/src/utils/result';
|
||||
|
||||
export interface ApiQueryOk {
|
||||
items: MediaTypeModel[];
|
||||
warnings: AppError[];
|
||||
warnings: MDBError[];
|
||||
}
|
||||
|
||||
export class APIManager {
|
||||
|
|
@ -24,14 +24,14 @@ export class APIManager {
|
|||
* @param query
|
||||
* @param apisToQuery
|
||||
*/
|
||||
async query(query: string, apisToQuery: string[]): Promise<Result<ApiQueryOk, AppError>> {
|
||||
async query(query: string, apisToQuery: string[]): Promise<Result<ApiQueryOk, MDBError>> {
|
||||
Logger.debug(`MDB | api manager queried with "${query}"`);
|
||||
|
||||
const apis = this.apis.filter(api => apisToQuery.includes(api.apiName));
|
||||
const results = await Promise.all(apis.map(api => api.searchByTitle(query)));
|
||||
|
||||
const items: MediaTypeModel[] = [];
|
||||
const warnings: AppError[] = [];
|
||||
const warnings: MDBError[] = [];
|
||||
for (const result of results) {
|
||||
if (result.ok) {
|
||||
items.push(...result.value);
|
||||
|
|
@ -43,8 +43,8 @@ export class APIManager {
|
|||
if (items.length === 0 && warnings.length > 0) {
|
||||
// If all APIs failed, surface an error (using the first as representative)
|
||||
return err(
|
||||
toAppError(warnings[0], {
|
||||
kind: AppErrorKind.Api,
|
||||
toMdbError(warnings[0], {
|
||||
kind: MDBErrorKind.Api,
|
||||
message: 'Failed to query APIs',
|
||||
userMessage: 'Failed to query APIs',
|
||||
context: { query, apisToQuery },
|
||||
|
|
@ -64,7 +64,7 @@ export class APIManager {
|
|||
*
|
||||
* @param item
|
||||
*/
|
||||
async queryDetailedInfo(item: MediaTypeModel): Promise<Result<MediaTypeModel | undefined, AppError>> {
|
||||
async queryDetailedInfo(item: MediaTypeModel): Promise<Result<MediaTypeModel | undefined, MDBError>> {
|
||||
return await this.queryDetailedInfoById(item.id, item.dataSource);
|
||||
}
|
||||
|
||||
|
|
@ -74,7 +74,7 @@ export class APIManager {
|
|||
* @param id
|
||||
* @param apiName
|
||||
*/
|
||||
async queryDetailedInfoById(id: string, apiName: string): Promise<Result<MediaTypeModel | undefined, AppError>> {
|
||||
async queryDetailedInfoById(id: string, apiName: string): Promise<Result<MediaTypeModel | undefined, MDBError>> {
|
||||
for (const api of this.apis) {
|
||||
if (api.apiName === apiName) {
|
||||
const result = await api.getById(id);
|
||||
|
|
@ -88,8 +88,8 @@ export class APIManager {
|
|||
}
|
||||
|
||||
return err(
|
||||
toAppError(new Error(`API not found: ${apiName}`), {
|
||||
kind: AppErrorKind.Validation,
|
||||
toMdbError(new Error(`API not found: ${apiName}`), {
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `API not found: ${apiName}`,
|
||||
userMessage: `API not found: ${apiName}`,
|
||||
context: { apiName, id },
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import type MediaDbPlugin from 'packages/obsidian/src/main';
|
||||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import type { MediaType } from 'packages/obsidian/src/utils/MediaType';
|
||||
import type { Result } from 'packages/obsidian/src/utils/result';
|
||||
|
||||
|
|
@ -16,9 +16,9 @@ export abstract class APIModel {
|
|||
*
|
||||
* @param title the title to query for
|
||||
*/
|
||||
abstract searchByTitle(title: string): Promise<Result<MediaTypeModel[], AppError>>;
|
||||
abstract searchByTitle(title: string): Promise<Result<MediaTypeModel[], MDBError>>;
|
||||
|
||||
abstract getById(id: string): Promise<Result<MediaTypeModel, AppError>>;
|
||||
abstract getById(id: string): Promise<Result<MediaTypeModel, MDBError>>;
|
||||
|
||||
abstract getDisabledMediaTypes(): MediaType[];
|
||||
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@ import { APIModel } from 'packages/obsidian/src/api/APIModel';
|
|||
import type MediaDbPlugin from 'packages/obsidian/src/main';
|
||||
import { BoardGameModel } from 'packages/obsidian/src/models/BoardGameModel';
|
||||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind, toAppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { Logger } from 'packages/obsidian/src/utils/Logger';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind, toMdbError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MediaType } from 'packages/obsidian/src/utils/MediaType';
|
||||
import type { Result } from 'packages/obsidian/src/utils/result';
|
||||
import { err, fromPromise, ok } from 'packages/obsidian/src/utils/result';
|
||||
|
|
@ -25,12 +25,12 @@ export class BoardGameGeekAPI extends APIModel {
|
|||
this.types = [MediaType.BoardGame];
|
||||
}
|
||||
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], AppError>> {
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by Title`);
|
||||
const key = this.plugin.app.secretStorage.getSecret(this.plugin.settings.BoardgameGeekKeyId);
|
||||
if (!key) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | API key for ${this.apiName} missing.`,
|
||||
userMessage: `API key for ${this.apiName} missing.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -46,8 +46,8 @@ export class BoardGameGeekAPI extends APIModel {
|
|||
},
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, title },
|
||||
|
|
@ -61,7 +61,7 @@ export class BoardGameGeekAPI extends APIModel {
|
|||
|
||||
if (fetchData.status === 401) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
userMessage: `Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -70,7 +70,7 @@ export class BoardGameGeekAPI extends APIModel {
|
|||
|
||||
if (fetchData.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: fetchData.status },
|
||||
|
|
@ -103,12 +103,12 @@ export class BoardGameGeekAPI extends APIModel {
|
|||
return ok(ret);
|
||||
}
|
||||
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, AppError>> {
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
const key = this.plugin.app.secretStorage.getSecret(this.plugin.settings.BoardgameGeekKeyId);
|
||||
if (!key) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | API key for ${this.apiName} missing.`,
|
||||
userMessage: `API key for ${this.apiName} missing.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -124,8 +124,8 @@ export class BoardGameGeekAPI extends APIModel {
|
|||
},
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -139,7 +139,7 @@ export class BoardGameGeekAPI extends APIModel {
|
|||
|
||||
if (fetchData.status === 401) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
userMessage: `Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -148,7 +148,7 @@ export class BoardGameGeekAPI extends APIModel {
|
|||
|
||||
if (fetchData.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: fetchData.status, id },
|
||||
|
|
@ -162,7 +162,7 @@ export class BoardGameGeekAPI extends APIModel {
|
|||
const boardgame = response.querySelector('boardgame');
|
||||
if (!boardgame) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received invalid data from ${this.apiName}.`,
|
||||
userMessage: `Received invalid data from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
|
|||
|
|
@ -5,9 +5,9 @@ import { APIModel } from 'packages/obsidian/src/api/APIModel';
|
|||
import type MediaDbPlugin from 'packages/obsidian/src/main';
|
||||
import { ComicMangaModel } from 'packages/obsidian/src/models/ComicMangaModel';
|
||||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind, toAppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { Logger } from 'packages/obsidian/src/utils/Logger';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind, toMdbError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MediaType } from 'packages/obsidian/src/utils/MediaType';
|
||||
import type { Result } from 'packages/obsidian/src/utils/result';
|
||||
import { err, fromPromise, ok } from 'packages/obsidian/src/utils/result';
|
||||
|
|
@ -27,12 +27,12 @@ export class ComicVineAPI extends APIModel {
|
|||
this.types = [MediaType.ComicManga];
|
||||
}
|
||||
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], AppError>> {
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by Title`);
|
||||
const key = this.plugin.app.secretStorage.getSecret(this.plugin.settings.ComicVineKeyId);
|
||||
if (!key) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | API key for ${this.apiName} missing.`,
|
||||
userMessage: `API key for ${this.apiName} missing.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -45,8 +45,8 @@ export class ComicVineAPI extends APIModel {
|
|||
url: searchUrl,
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, title },
|
||||
|
|
@ -59,7 +59,7 @@ export class ComicVineAPI extends APIModel {
|
|||
// console.debug(fetchData);
|
||||
if (fetchData.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: fetchData.status },
|
||||
|
|
@ -85,12 +85,12 @@ export class ComicVineAPI extends APIModel {
|
|||
return ok(ret);
|
||||
}
|
||||
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, AppError>> {
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
const key = this.plugin.app.secretStorage.getSecret(this.plugin.settings.ComicVineKeyId);
|
||||
if (!key) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | API key for ${this.apiName} missing.`,
|
||||
userMessage: `API key for ${this.apiName} missing.`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -103,8 +103,8 @@ export class ComicVineAPI extends APIModel {
|
|||
url: searchUrl,
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -119,7 +119,7 @@ export class ComicVineAPI extends APIModel {
|
|||
|
||||
if (fetchData.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: fetchData.status, id },
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@ import { APIModel } from 'packages/obsidian/src/api/APIModel';
|
|||
import type MediaDbPlugin from 'packages/obsidian/src/main';
|
||||
import { GameModel } from 'packages/obsidian/src/models/GameModel';
|
||||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind, toAppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { Logger } from 'packages/obsidian/src/utils/Logger';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind, toMdbError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MediaType } from 'packages/obsidian/src/utils/MediaType';
|
||||
import type { Result } from 'packages/obsidian/src/utils/result';
|
||||
import { err, fromPromise, ok } from 'packages/obsidian/src/utils/result';
|
||||
|
|
@ -60,7 +60,7 @@ export class IGDBAPI extends APIModel {
|
|||
this.types = [MediaType.Game];
|
||||
}
|
||||
|
||||
private async getAuthToken(): Promise<Result<string, AppError>> {
|
||||
private async getAuthToken(): Promise<Result<string, MDBError>> {
|
||||
const currentTime = Date.now();
|
||||
if (this.accessToken && currentTime < this.tokenExpiry) return ok(this.accessToken);
|
||||
|
||||
|
|
@ -69,7 +69,7 @@ export class IGDBAPI extends APIModel {
|
|||
|
||||
if (!clientId || !clientSecret) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | Client ID or Client Secret for ${this.apiName} missing.`,
|
||||
userMessage: `Client ID or Client Secret for ${this.apiName} missing.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -82,8 +82,8 @@ export class IGDBAPI extends APIModel {
|
|||
method: 'POST',
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying Twitch auth for ${this.apiName}`,
|
||||
userMessage: `Network error querying Twitch auth for ${this.apiName}`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -96,7 +96,7 @@ export class IGDBAPI extends APIModel {
|
|||
const response = responseResult.value;
|
||||
if (response.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Auth failed for ${this.apiName}. Check Credentials.`,
|
||||
userMessage: `Auth failed for ${this.apiName}. Check Credentials.`,
|
||||
context: { apiName: this.apiName, status: response.status },
|
||||
|
|
@ -109,12 +109,12 @@ export class IGDBAPI extends APIModel {
|
|||
return ok(this.accessToken);
|
||||
}
|
||||
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], AppError>> {
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by Title`);
|
||||
const clientId = this.plugin.app.secretStorage.getSecret(this.plugin.settings.IGDBClientId);
|
||||
if (!clientId) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | Client ID for ${this.apiName} missing.`,
|
||||
userMessage: `Client ID for ${this.apiName} missing.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -134,8 +134,8 @@ export class IGDBAPI extends APIModel {
|
|||
body: queryBody,
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, title },
|
||||
|
|
@ -147,7 +147,7 @@ export class IGDBAPI extends APIModel {
|
|||
const response = responseResult.value;
|
||||
if (response.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${response.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: response.status },
|
||||
|
|
@ -172,12 +172,12 @@ export class IGDBAPI extends APIModel {
|
|||
);
|
||||
}
|
||||
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, AppError>> {
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
const clientId = this.plugin.app.secretStorage.getSecret(this.plugin.settings.IGDBClientId);
|
||||
if (!clientId) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | Client ID for ${this.apiName} missing.`,
|
||||
userMessage: `Client ID for ${this.apiName} missing.`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -197,8 +197,8 @@ export class IGDBAPI extends APIModel {
|
|||
body: queryBody,
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -210,7 +210,7 @@ export class IGDBAPI extends APIModel {
|
|||
const response = responseResult.value;
|
||||
if (response.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${response.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: response.status, id },
|
||||
|
|
@ -220,7 +220,7 @@ export class IGDBAPI extends APIModel {
|
|||
const data = response.json as IGDBGame[];
|
||||
if (!data || data.length === 0) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | No result found for ID ${id}`,
|
||||
userMessage: `No result found for ID ${id}`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
|
|||
|
|
@ -4,9 +4,9 @@ import type MediaDbPlugin from 'packages/obsidian/src/main';
|
|||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import { MovieModel } from 'packages/obsidian/src/models/MovieModel';
|
||||
import { SeriesModel } from 'packages/obsidian/src/models/SeriesModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind } from 'packages/obsidian/src/utils/AppError';
|
||||
import { Logger } from 'packages/obsidian/src/utils/Logger';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MediaType } from 'packages/obsidian/src/utils/MediaType';
|
||||
import type { Result } from 'packages/obsidian/src/utils/result';
|
||||
import { err, ok } from 'packages/obsidian/src/utils/result';
|
||||
|
|
@ -33,7 +33,7 @@ export class MALAPI extends APIModel {
|
|||
this.typeMappings.set('ova', 'ova');
|
||||
}
|
||||
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], AppError>> {
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by Title`);
|
||||
|
||||
const client = createClient<paths>({ baseUrl: 'https://api.jikan.moe/v4/' });
|
||||
|
|
@ -51,7 +51,7 @@ export class MALAPI extends APIModel {
|
|||
|
||||
if (response.error !== undefined) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: response.response.status },
|
||||
|
|
@ -108,7 +108,7 @@ export class MALAPI extends APIModel {
|
|||
return ok(ret);
|
||||
}
|
||||
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, AppError>> {
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
|
||||
const client = createClient<paths>({ baseUrl: 'https://api.jikan.moe/v4/' });
|
||||
|
|
@ -124,7 +124,7 @@ export class MALAPI extends APIModel {
|
|||
|
||||
if (response.error !== undefined) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: response.response.status, id },
|
||||
|
|
@ -135,7 +135,7 @@ export class MALAPI extends APIModel {
|
|||
|
||||
if (result === undefined) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | No data found for ID ${id} in ${this.apiName}.`,
|
||||
userMessage: `No data found for ID ${id} in ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -248,7 +248,7 @@ export class MALAPI extends APIModel {
|
|||
}
|
||||
|
||||
return err({
|
||||
kind: AppErrorKind.Unexpected,
|
||||
kind: MDBErrorKind.Unexpected,
|
||||
message: `MDB | Unknown media type for id ${id}`,
|
||||
userMessage: `Unknown media type for id ${id}`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@ import { APIModel } from 'packages/obsidian/src/api/APIModel';
|
|||
import type MediaDbPlugin from 'packages/obsidian/src/main';
|
||||
import { ComicMangaModel } from 'packages/obsidian/src/models/ComicMangaModel';
|
||||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind } from 'packages/obsidian/src/utils/AppError';
|
||||
import { Logger } from 'packages/obsidian/src/utils/Logger';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MediaType } from 'packages/obsidian/src/utils/MediaType';
|
||||
import type { Result } from 'packages/obsidian/src/utils/result';
|
||||
import { err, ok } from 'packages/obsidian/src/utils/result';
|
||||
|
|
@ -35,7 +35,7 @@ export class MALAPIManga extends APIModel {
|
|||
this.typeMappings.set('novel', 'novel');
|
||||
}
|
||||
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], AppError>> {
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by Title`);
|
||||
|
||||
const client = createClient<paths>({ baseUrl: 'https://api.jikan.moe/v4/' });
|
||||
|
|
@ -53,7 +53,7 @@ export class MALAPIManga extends APIModel {
|
|||
|
||||
if (response.error !== undefined) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: response.response.status },
|
||||
|
|
@ -106,7 +106,7 @@ export class MALAPIManga extends APIModel {
|
|||
return ok(ret);
|
||||
}
|
||||
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, AppError>> {
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
|
||||
const client = createClient<paths>({ baseUrl: 'https://api.jikan.moe/v4/' });
|
||||
|
|
@ -122,7 +122,7 @@ export class MALAPIManga extends APIModel {
|
|||
|
||||
if (response.error !== undefined) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: response.response.status, id },
|
||||
|
|
@ -133,7 +133,7 @@ export class MALAPIManga extends APIModel {
|
|||
|
||||
if (!result) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | No data found for ID ${id} in ${this.apiName}.`,
|
||||
userMessage: `No data found for ID ${id} in ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@ import { APIModel } from 'packages/obsidian/src/api/APIModel';
|
|||
import type MediaDbPlugin from 'packages/obsidian/src/main';
|
||||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import { MusicReleaseModel } from 'packages/obsidian/src/models/MusicReleaseModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind, toAppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { Logger } from 'packages/obsidian/src/utils/Logger';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind, toMdbError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MediaType } from 'packages/obsidian/src/utils/MediaType';
|
||||
import type { Result } from 'packages/obsidian/src/utils/result';
|
||||
import { err, fromPromise, ok } from 'packages/obsidian/src/utils/result';
|
||||
|
|
@ -108,7 +108,7 @@ export class MusicBrainzAPI extends APIModel {
|
|||
this.types = [MediaType.MusicRelease];
|
||||
}
|
||||
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], AppError>> {
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by Title`);
|
||||
|
||||
const searchUrl = `https://musicbrainz.org/ws/2/release-group?query=${encodeURIComponent(title)}&limit=20&fmt=json`;
|
||||
|
|
@ -121,8 +121,8 @@ export class MusicBrainzAPI extends APIModel {
|
|||
},
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, title },
|
||||
|
|
@ -137,7 +137,7 @@ export class MusicBrainzAPI extends APIModel {
|
|||
|
||||
if (fetchData.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: fetchData.status },
|
||||
|
|
@ -172,7 +172,7 @@ export class MusicBrainzAPI extends APIModel {
|
|||
return ok(ret);
|
||||
}
|
||||
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, AppError>> {
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
|
||||
// Fetch release group
|
||||
|
|
@ -185,8 +185,8 @@ export class MusicBrainzAPI extends APIModel {
|
|||
},
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -199,7 +199,7 @@ export class MusicBrainzAPI extends APIModel {
|
|||
|
||||
if (groupResponse.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${groupResponse.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${groupResponse.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: groupResponse.status, id },
|
||||
|
|
@ -212,7 +212,7 @@ export class MusicBrainzAPI extends APIModel {
|
|||
const firstRelease = result.releases?.[0];
|
||||
if (!firstRelease) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: 'MDB | No releases found in release group.',
|
||||
userMessage: 'No releases found in release group.',
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -231,8 +231,8 @@ export class MusicBrainzAPI extends APIModel {
|
|||
},
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, id, releaseId: firstRelease.id },
|
||||
|
|
@ -245,7 +245,7 @@ export class MusicBrainzAPI extends APIModel {
|
|||
|
||||
if (releaseResponse.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${releaseResponse.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${releaseResponse.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: releaseResponse.status, id, releaseId: firstRelease.id },
|
||||
|
|
|
|||
|
|
@ -5,9 +5,9 @@ import { GameModel } from 'packages/obsidian/src/models/GameModel';
|
|||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import { MovieModel } from 'packages/obsidian/src/models/MovieModel';
|
||||
import { SeriesModel } from 'packages/obsidian/src/models/SeriesModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind, toAppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { Logger } from 'packages/obsidian/src/utils/Logger';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind, toMdbError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MediaType } from 'packages/obsidian/src/utils/MediaType';
|
||||
import type { Result } from 'packages/obsidian/src/utils/result';
|
||||
import { err, fromPromise, ok } from 'packages/obsidian/src/utils/result';
|
||||
|
|
@ -79,13 +79,13 @@ export class OMDbAPI extends APIModel {
|
|||
this.typeMappings.set('game', 'game');
|
||||
}
|
||||
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], AppError>> {
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by Title`);
|
||||
const key = this.plugin.app.secretStorage.getSecret(this.plugin.settings.OMDbKeyId);
|
||||
|
||||
if (!key) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | API key for ${this.apiName} missing.`,
|
||||
userMessage: `API key for ${this.apiName} missing.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -98,8 +98,8 @@ export class OMDbAPI extends APIModel {
|
|||
method: 'GET',
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, title },
|
||||
|
|
@ -113,7 +113,7 @@ export class OMDbAPI extends APIModel {
|
|||
|
||||
if (response.status === 401) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
userMessage: `Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -121,7 +121,7 @@ export class OMDbAPI extends APIModel {
|
|||
}
|
||||
if (response.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${response.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: response.status },
|
||||
|
|
@ -132,7 +132,7 @@ export class OMDbAPI extends APIModel {
|
|||
|
||||
if (!data) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | No data received from ${this.apiName}.`,
|
||||
userMessage: `No data received from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -145,7 +145,7 @@ export class OMDbAPI extends APIModel {
|
|||
}
|
||||
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received error from ${this.apiName}: ${data.Error}`,
|
||||
userMessage: `${data.Error}`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -203,13 +203,13 @@ export class OMDbAPI extends APIModel {
|
|||
return ok(ret);
|
||||
}
|
||||
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, AppError>> {
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
const key = this.plugin.app.secretStorage.getSecret(this.plugin.settings.OMDbKeyId);
|
||||
|
||||
if (!key) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | API key for ${this.apiName} missing.`,
|
||||
userMessage: `API key for ${this.apiName} missing.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -222,8 +222,8 @@ export class OMDbAPI extends APIModel {
|
|||
method: 'GET',
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -237,7 +237,7 @@ export class OMDbAPI extends APIModel {
|
|||
|
||||
if (response.status === 401) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
userMessage: `Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -245,7 +245,7 @@ export class OMDbAPI extends APIModel {
|
|||
}
|
||||
if (response.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${response.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: response.status, id },
|
||||
|
|
@ -256,7 +256,7 @@ export class OMDbAPI extends APIModel {
|
|||
|
||||
if (!result) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | No data received from ${this.apiName}.`,
|
||||
userMessage: `No data received from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -265,7 +265,7 @@ export class OMDbAPI extends APIModel {
|
|||
|
||||
if (result.Response === 'False') {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received error from ${this.apiName}: ${result.Error}`,
|
||||
userMessage: `${result.Error}`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -275,7 +275,7 @@ export class OMDbAPI extends APIModel {
|
|||
const type = this.typeMappings.get(result.Type.toLowerCase());
|
||||
if (type === undefined) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `${result.Type.toLowerCase()} is an unsupported type.`,
|
||||
userMessage: `${result.Type.toLowerCase()} is an unsupported type.`,
|
||||
context: { apiName: this.apiName, id, type: result.Type },
|
||||
|
|
@ -375,7 +375,7 @@ export class OMDbAPI extends APIModel {
|
|||
}
|
||||
|
||||
return err({
|
||||
kind: AppErrorKind.Unexpected,
|
||||
kind: MDBErrorKind.Unexpected,
|
||||
message: `MDB | Unknown media type for id ${id}`,
|
||||
userMessage: `Unknown media type for id ${id}`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@ import { APIModel } from 'packages/obsidian/src/api/APIModel';
|
|||
import type MediaDbPlugin from 'packages/obsidian/src/main';
|
||||
import { BookModel } from 'packages/obsidian/src/models/BookModel';
|
||||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind, toAppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { Logger } from 'packages/obsidian/src/utils/Logger';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind, toMdbError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MediaType } from 'packages/obsidian/src/utils/MediaType';
|
||||
import type { Result } from 'packages/obsidian/src/utils/result';
|
||||
import { err, fromPromise, ok } from 'packages/obsidian/src/utils/result';
|
||||
|
|
@ -48,7 +48,7 @@ export class OpenLibraryAPI extends APIModel {
|
|||
this.types = [MediaType.Book];
|
||||
}
|
||||
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], AppError>> {
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by Title`);
|
||||
|
||||
const client = createClient<paths>({ baseUrl: 'https://openlibrary.org/' });
|
||||
|
|
@ -63,8 +63,8 @@ export class OpenLibraryAPI extends APIModel {
|
|||
fetch: obsidianFetch,
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, title },
|
||||
|
|
@ -78,7 +78,7 @@ export class OpenLibraryAPI extends APIModel {
|
|||
|
||||
if (response.error !== undefined) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: response.response.status },
|
||||
|
|
@ -109,7 +109,7 @@ export class OpenLibraryAPI extends APIModel {
|
|||
return ok(ret);
|
||||
}
|
||||
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, AppError>> {
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
|
||||
const client = createClient<paths>({ baseUrl: 'https://openlibrary.org/' });
|
||||
|
|
@ -125,8 +125,8 @@ export class OpenLibraryAPI extends APIModel {
|
|||
fetch: obsidianFetch,
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -140,7 +140,7 @@ export class OpenLibraryAPI extends APIModel {
|
|||
|
||||
if (response.error !== undefined) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: response.response.status, id },
|
||||
|
|
@ -155,7 +155,7 @@ export class OpenLibraryAPI extends APIModel {
|
|||
const result = data.docs?.[0];
|
||||
if (!result) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | No data found for ID ${id} in ${this.apiName}.`,
|
||||
userMessage: `No data found for ID ${id}.`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ import { APIModel } from 'packages/obsidian/src/api/APIModel';
|
|||
import type MediaDbPlugin from 'packages/obsidian/src/main';
|
||||
import { GameModel } from 'packages/obsidian/src/models/GameModel';
|
||||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind, toAppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind, toMdbError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MediaType } from 'packages/obsidian/src/utils/MediaType';
|
||||
import type { Result } from 'packages/obsidian/src/utils/result';
|
||||
import { err, fromPromise, ok } from 'packages/obsidian/src/utils/result';
|
||||
|
|
@ -40,11 +40,11 @@ export class RAWGAPI extends APIModel {
|
|||
this.types = [MediaType.Game];
|
||||
}
|
||||
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], AppError>> {
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], MDBError>> {
|
||||
const key = this.plugin.app.secretStorage.getSecret(this.plugin.settings.RAWGAPIKeyId);
|
||||
if (!key) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | API key for ${this.apiName} missing.`,
|
||||
userMessage: `API key for ${this.apiName} missing.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -57,8 +57,8 @@ export class RAWGAPI extends APIModel {
|
|||
method: 'GET',
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, title },
|
||||
|
|
@ -71,7 +71,7 @@ export class RAWGAPI extends APIModel {
|
|||
const response = responseResult.value;
|
||||
if (response.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Error ${response.status} from ${this.apiName}.`,
|
||||
userMessage: `Error ${response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: response.status },
|
||||
|
|
@ -95,11 +95,11 @@ export class RAWGAPI extends APIModel {
|
|||
);
|
||||
}
|
||||
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, AppError>> {
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, MDBError>> {
|
||||
const key = this.plugin.app.secretStorage.getSecret(this.plugin.settings.RAWGAPIKeyId);
|
||||
if (!key) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | API key for ${this.apiName} missing.`,
|
||||
userMessage: `API key for ${this.apiName} missing.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -112,8 +112,8 @@ export class RAWGAPI extends APIModel {
|
|||
method: 'GET',
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -126,7 +126,7 @@ export class RAWGAPI extends APIModel {
|
|||
const response = responseResult.value;
|
||||
if (response.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Error ${response.status} from ${this.apiName}.`,
|
||||
userMessage: `Error ${response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: response.status, id },
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@ import { APIModel } from 'packages/obsidian/src/api/APIModel';
|
|||
import type MediaDbPlugin from 'packages/obsidian/src/main';
|
||||
import { GameModel } from 'packages/obsidian/src/models/GameModel';
|
||||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind, toAppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { Logger } from 'packages/obsidian/src/utils/Logger';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind, toMdbError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MediaType } from 'packages/obsidian/src/utils/MediaType';
|
||||
import type { Result } from 'packages/obsidian/src/utils/result';
|
||||
import { err, fromPromise, ok } from 'packages/obsidian/src/utils/result';
|
||||
|
|
@ -148,7 +148,7 @@ export class SteamAPI extends APIModel {
|
|||
this.typeMappings.set('game', 'game');
|
||||
}
|
||||
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], AppError>> {
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by Title`);
|
||||
|
||||
const searchUrl = `https://steamcommunity.com/actions/SearchApps/${encodeURIComponent(title)}`;
|
||||
|
|
@ -157,8 +157,8 @@ export class SteamAPI extends APIModel {
|
|||
url: searchUrl,
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, title },
|
||||
|
|
@ -171,7 +171,7 @@ export class SteamAPI extends APIModel {
|
|||
|
||||
if (fetchData.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: fetchData.status },
|
||||
|
|
@ -200,7 +200,7 @@ export class SteamAPI extends APIModel {
|
|||
return ok(ret);
|
||||
}
|
||||
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, AppError>> {
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
|
||||
const searchUrl = `https://store.steampowered.com/api/appdetails?appids=${encodeURIComponent(id)}&l=en`;
|
||||
|
|
@ -209,8 +209,8 @@ export class SteamAPI extends APIModel {
|
|||
url: searchUrl,
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -223,7 +223,7 @@ export class SteamAPI extends APIModel {
|
|||
|
||||
if (fetchData.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: fetchData.status, id },
|
||||
|
|
@ -242,7 +242,7 @@ export class SteamAPI extends APIModel {
|
|||
}
|
||||
if (!result) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: 'MDB | API returned invalid data.',
|
||||
userMessage: 'Steam returned invalid data.',
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -254,8 +254,8 @@ export class SteamAPI extends APIModel {
|
|||
// Check if a poster version of the image exists, else use the header image
|
||||
const imageUrl = `https://steamcdn-a.akamaihd.net/steam/apps/${result.steam_appid}/library_600x900_2x.jpg`;
|
||||
const existsResult = await fromPromise(imageUrlExists(imageUrl), cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Failed to validate image URL for ${this.apiName}`,
|
||||
userMessage: `Failed to validate image URL for ${this.apiName}`,
|
||||
context: { apiName: this.apiName, id, imageUrl },
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@ import { APIModel } from 'packages/obsidian/src/api/APIModel';
|
|||
import type MediaDbPlugin from 'packages/obsidian/src/main';
|
||||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import { MovieModel } from 'packages/obsidian/src/models/MovieModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind, toAppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { Logger } from 'packages/obsidian/src/utils/Logger';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind, toMdbError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MediaType } from 'packages/obsidian/src/utils/MediaType';
|
||||
import type { Result } from 'packages/obsidian/src/utils/result';
|
||||
import { err, fromPromise, ok } from 'packages/obsidian/src/utils/result';
|
||||
|
|
@ -59,13 +59,13 @@ export class TMDBMovieAPI extends APIModel {
|
|||
this.typeMappings.set('movie', 'movie');
|
||||
}
|
||||
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], AppError>> {
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by Title`);
|
||||
const key = this.plugin.app.secretStorage.getSecret(this.plugin.settings.TMDBKeyId);
|
||||
|
||||
if (!key) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | API key for ${this.apiName} missing.`,
|
||||
userMessage: `API key for ${this.apiName} missing.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -87,8 +87,8 @@ export class TMDBMovieAPI extends APIModel {
|
|||
fetch: obsidianFetch,
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, title },
|
||||
|
|
@ -103,7 +103,7 @@ export class TMDBMovieAPI extends APIModel {
|
|||
|
||||
if (response.response.status === 401) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
userMessage: `Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -111,7 +111,7 @@ export class TMDBMovieAPI extends APIModel {
|
|||
}
|
||||
if (response.response.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: response.response.status },
|
||||
|
|
@ -122,7 +122,7 @@ export class TMDBMovieAPI extends APIModel {
|
|||
|
||||
if (!data) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | No data received from ${this.apiName}.`,
|
||||
userMessage: `No data received from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -153,13 +153,13 @@ export class TMDBMovieAPI extends APIModel {
|
|||
return ok(ret);
|
||||
}
|
||||
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, AppError>> {
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
const key = this.plugin.app.secretStorage.getSecret(this.plugin.settings.TMDBKeyId);
|
||||
|
||||
if (!key) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | API key for ${this.apiName} missing.`,
|
||||
userMessage: `API key for ${this.apiName} missing.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -181,8 +181,8 @@ export class TMDBMovieAPI extends APIModel {
|
|||
fetch: obsidianFetch,
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -197,7 +197,7 @@ export class TMDBMovieAPI extends APIModel {
|
|||
|
||||
if (response.response.status === 401) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
userMessage: `Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -205,7 +205,7 @@ export class TMDBMovieAPI extends APIModel {
|
|||
}
|
||||
if (response.response.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: response.response.status, id },
|
||||
|
|
@ -216,7 +216,7 @@ export class TMDBMovieAPI extends APIModel {
|
|||
|
||||
if (!result) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | No data received from ${this.apiName}.`,
|
||||
userMessage: `No data received from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@ import { APIModel } from 'packages/obsidian/src/api/APIModel';
|
|||
import type MediaDbPlugin from 'packages/obsidian/src/main';
|
||||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import { SeasonModel } from 'packages/obsidian/src/models/SeasonModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind, toAppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { Logger } from 'packages/obsidian/src/utils/Logger';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind, toMdbError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MediaType } from 'packages/obsidian/src/utils/MediaType';
|
||||
import type { Result } from 'packages/obsidian/src/utils/result';
|
||||
import { err, fromPromise, ok } from 'packages/obsidian/src/utils/result';
|
||||
|
|
@ -63,13 +63,13 @@ export class TMDBSeasonAPI extends APIModel {
|
|||
this.typeMappings.set('tv', 'season');
|
||||
}
|
||||
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], AppError>> {
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by Title`);
|
||||
const key = this.plugin.app.secretStorage.getSecret(this.plugin.settings.TMDBKeyId);
|
||||
|
||||
if (!key) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | API key for ${this.apiName} missing.`,
|
||||
userMessage: `API key for ${this.apiName} missing.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -91,8 +91,8 @@ export class TMDBSeasonAPI extends APIModel {
|
|||
fetch: obsidianFetch,
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, title },
|
||||
|
|
@ -106,7 +106,7 @@ export class TMDBSeasonAPI extends APIModel {
|
|||
|
||||
if (searchResponse.response.status === 401) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
userMessage: `Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -115,7 +115,7 @@ export class TMDBSeasonAPI extends APIModel {
|
|||
|
||||
if (searchResponse.response.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${searchResponse.response.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${searchResponse.response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: searchResponse.response.status },
|
||||
|
|
@ -169,11 +169,11 @@ export class TMDBSeasonAPI extends APIModel {
|
|||
}
|
||||
|
||||
// Fetch all seasons for a given series
|
||||
async getSeasonsForSeries(tvId: string): Promise<Result<SeasonModel[], AppError>> {
|
||||
async getSeasonsForSeries(tvId: string): Promise<Result<SeasonModel[], MDBError>> {
|
||||
const key = this.plugin.app.secretStorage.getSecret(this.plugin.settings.TMDBKeyId);
|
||||
if (!key) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | API key for ${this.apiName} missing.`,
|
||||
userMessage: `API key for ${this.apiName} missing.`,
|
||||
context: { apiName: this.apiName, tvId },
|
||||
|
|
@ -192,8 +192,8 @@ export class TMDBSeasonAPI extends APIModel {
|
|||
fetch: obsidianFetch,
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, tvId },
|
||||
|
|
@ -207,7 +207,7 @@ export class TMDBSeasonAPI extends APIModel {
|
|||
|
||||
if (seriesResponse.response.status === 401) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
userMessage: `Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
context: { apiName: this.apiName, tvId },
|
||||
|
|
@ -216,7 +216,7 @@ export class TMDBSeasonAPI extends APIModel {
|
|||
|
||||
if (seriesResponse.response.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${seriesResponse.response.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${seriesResponse.response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: seriesResponse.response.status, tvId },
|
||||
|
|
@ -250,13 +250,13 @@ export class TMDBSeasonAPI extends APIModel {
|
|||
return ok(ret);
|
||||
}
|
||||
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, AppError>> {
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
const key = this.plugin.app.secretStorage.getSecret(this.plugin.settings.TMDBKeyId);
|
||||
|
||||
if (!key) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | API key for ${this.apiName} missing.`,
|
||||
userMessage: `API key for ${this.apiName} missing.`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -267,7 +267,7 @@ export class TMDBSeasonAPI extends APIModel {
|
|||
const m = /^(\d+)\/season\/(\d+)$/.exec(id);
|
||||
if (!m) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | Invalid season id "${id}". Expected format "<series_id>/season/<season_number>".`,
|
||||
userMessage: `Invalid season id "${id}".`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -294,8 +294,8 @@ export class TMDBSeasonAPI extends APIModel {
|
|||
fetch: obsidianFetch,
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -309,7 +309,7 @@ export class TMDBSeasonAPI extends APIModel {
|
|||
|
||||
if (seasonResponse.response.status === 401) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
userMessage: `Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -318,7 +318,7 @@ export class TMDBSeasonAPI extends APIModel {
|
|||
|
||||
if (seasonResponse.response.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${seasonResponse.response.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${seasonResponse.response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: seasonResponse.response.status, id },
|
||||
|
|
@ -328,7 +328,7 @@ export class TMDBSeasonAPI extends APIModel {
|
|||
const seasonData = seasonResponse.data;
|
||||
if (!seasonData) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | No data received from ${this.apiName}.`,
|
||||
userMessage: `No data received from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -349,8 +349,8 @@ export class TMDBSeasonAPI extends APIModel {
|
|||
fetch: obsidianFetch,
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -364,7 +364,7 @@ export class TMDBSeasonAPI extends APIModel {
|
|||
|
||||
if (seriesResponse.response.status === 401) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
userMessage: `Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -373,7 +373,7 @@ export class TMDBSeasonAPI extends APIModel {
|
|||
|
||||
if (seriesResponse.response.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${seriesResponse.response.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${seriesResponse.response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: seriesResponse.response.status, id },
|
||||
|
|
@ -384,7 +384,7 @@ export class TMDBSeasonAPI extends APIModel {
|
|||
|
||||
if (!seriesData) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | No data received from ${this.apiName}.`,
|
||||
userMessage: `No data received from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@ import { APIModel } from 'packages/obsidian/src/api/APIModel';
|
|||
import type MediaDbPlugin from 'packages/obsidian/src/main';
|
||||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import { SeriesModel } from 'packages/obsidian/src/models/SeriesModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind, toAppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { Logger } from 'packages/obsidian/src/utils/Logger';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind, toMdbError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MediaType } from 'packages/obsidian/src/utils/MediaType';
|
||||
import type { Result } from 'packages/obsidian/src/utils/result';
|
||||
import { err, fromPromise, ok } from 'packages/obsidian/src/utils/result';
|
||||
|
|
@ -50,13 +50,13 @@ export class TMDBSeriesAPI extends APIModel {
|
|||
this.typeMappings.set('tv', 'series');
|
||||
}
|
||||
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], AppError>> {
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by Title`);
|
||||
|
||||
const key = this.plugin.app.secretStorage.getSecret(this.plugin.settings.TMDBKeyId);
|
||||
if (!key) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | API key for ${this.apiName} missing.`,
|
||||
userMessage: `API key for ${this.apiName} missing.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -78,8 +78,8 @@ export class TMDBSeriesAPI extends APIModel {
|
|||
fetch: obsidianFetch,
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, title },
|
||||
|
|
@ -93,7 +93,7 @@ export class TMDBSeriesAPI extends APIModel {
|
|||
|
||||
if (response.response.status === 401) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
userMessage: `Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -101,7 +101,7 @@ export class TMDBSeriesAPI extends APIModel {
|
|||
}
|
||||
if (response.response.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: response.response.status },
|
||||
|
|
@ -112,7 +112,7 @@ export class TMDBSeriesAPI extends APIModel {
|
|||
|
||||
if (!data) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | No data received from ${this.apiName}.`,
|
||||
userMessage: `No data received from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -143,13 +143,13 @@ export class TMDBSeriesAPI extends APIModel {
|
|||
return ok(ret);
|
||||
}
|
||||
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, AppError>> {
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
const key = this.plugin.app.secretStorage.getSecret(this.plugin.settings.TMDBKeyId);
|
||||
|
||||
if (!key) {
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | API key for ${this.apiName} missing.`,
|
||||
userMessage: `API key for ${this.apiName} missing.`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -171,8 +171,8 @@ export class TMDBSeriesAPI extends APIModel {
|
|||
fetch: obsidianFetch,
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -186,7 +186,7 @@ export class TMDBSeriesAPI extends APIModel {
|
|||
|
||||
if (response.response.status === 401) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
userMessage: `Authentication for ${this.apiName} failed. Check the API key.`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -194,7 +194,7 @@ export class TMDBSeriesAPI extends APIModel {
|
|||
}
|
||||
if (response.response.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${response.response.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: response.response.status, id },
|
||||
|
|
@ -205,7 +205,7 @@ export class TMDBSeriesAPI extends APIModel {
|
|||
|
||||
if (!result) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | No data received from ${this.apiName}.`,
|
||||
userMessage: `No data received from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@ import { APIModel } from 'packages/obsidian/src/api/APIModel';
|
|||
import type MediaDbPlugin from 'packages/obsidian/src/main';
|
||||
import { GameModel } from 'packages/obsidian/src/models/GameModel';
|
||||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind, toAppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { Logger } from 'packages/obsidian/src/utils/Logger';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind, toMdbError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MediaType } from 'packages/obsidian/src/utils/MediaType';
|
||||
import type { Result } from 'packages/obsidian/src/utils/result';
|
||||
import { err, fromPromise, ok } from 'packages/obsidian/src/utils/result';
|
||||
|
|
@ -110,7 +110,7 @@ export class VNDBAPI extends APIModel {
|
|||
* @returns A JSON object representing the query response.
|
||||
* @see {@link https://api.vndb.org/kana#api-structure}
|
||||
*/
|
||||
private async postQuery(endpoint: string, body: string): Promise<Result<unknown, AppError>> {
|
||||
private async postQuery(endpoint: string, body: string): Promise<Result<unknown, MDBError>> {
|
||||
const fetchDataResult = await fromPromise(
|
||||
requestUrl({
|
||||
url: `${this.apiUrl}${endpoint}`,
|
||||
|
|
@ -120,8 +120,8 @@ export class VNDBAPI extends APIModel {
|
|||
throw: false,
|
||||
}),
|
||||
cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Network error querying ${this.apiName}`,
|
||||
userMessage: `Network error querying ${this.apiName}`,
|
||||
context: { apiName: this.apiName, endpoint },
|
||||
|
|
@ -136,42 +136,42 @@ export class VNDBAPI extends APIModel {
|
|||
switch (fetchData.status) {
|
||||
case 400:
|
||||
return err({
|
||||
kind: AppErrorKind.Validation,
|
||||
kind: MDBErrorKind.Validation,
|
||||
message: `MDB | Invalid request body or query [${fetchData.text}].`,
|
||||
userMessage: 'Invalid VNDB request.',
|
||||
context: { apiName: this.apiName, endpoint, status: fetchData.status },
|
||||
});
|
||||
case 404:
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: 'MDB | Invalid API path or HTTP method.',
|
||||
userMessage: 'VNDB endpoint not found.',
|
||||
context: { apiName: this.apiName, endpoint, status: fetchData.status },
|
||||
});
|
||||
case 429:
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: 'MDB | VNDB throttled the request.',
|
||||
userMessage: 'VNDB throttled the request. Please try again later.',
|
||||
context: { apiName: this.apiName, endpoint, status: fetchData.status },
|
||||
});
|
||||
case 500:
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: 'MDB | VNDB server error.',
|
||||
userMessage: 'VNDB server error.',
|
||||
context: { apiName: this.apiName, endpoint, status: fetchData.status },
|
||||
});
|
||||
case 502:
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: 'MDB | VNDB server is down.',
|
||||
userMessage: 'VNDB server is down.',
|
||||
context: { apiName: this.apiName, endpoint, status: fetchData.status },
|
||||
});
|
||||
default:
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, endpoint, status: fetchData.status },
|
||||
|
|
@ -187,7 +187,7 @@ export class VNDBAPI extends APIModel {
|
|||
* Queries visual novel entries.
|
||||
* @see {@link https://api.vndb.org/kana#post-vn}
|
||||
*/
|
||||
private async postVNQuery(body: string): Promise<Result<VNJSONResponse, AppError>> {
|
||||
private async postVNQuery(body: string): Promise<Result<VNJSONResponse, MDBError>> {
|
||||
const result = await this.postQuery('/vn', body);
|
||||
return result.ok ? ok(result.value as VNJSONResponse) : err(result.error);
|
||||
}
|
||||
|
|
@ -197,12 +197,12 @@ export class VNDBAPI extends APIModel {
|
|||
* Queries release entries.
|
||||
* @see {@link https://api.vndb.org/kana#post-release}
|
||||
*/
|
||||
private async postReleaseQuery(body: string): Promise<Result<ReleaseJSONResponse, AppError>> {
|
||||
private async postReleaseQuery(body: string): Promise<Result<ReleaseJSONResponse, MDBError>> {
|
||||
const result = await this.postQuery('/release', body);
|
||||
return result.ok ? ok(result.value as ReleaseJSONResponse) : err(result.error);
|
||||
}
|
||||
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], AppError>> {
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by Title`);
|
||||
|
||||
/* SFW Filter: has ANY official&&complete&&standalone&&SFW release
|
||||
|
|
@ -253,7 +253,7 @@ export class VNDBAPI extends APIModel {
|
|||
return ok(ret);
|
||||
}
|
||||
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, AppError>> {
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
|
||||
const vnDataResult = await this.postVNQuery(`{
|
||||
|
|
@ -267,7 +267,7 @@ export class VNDBAPI extends APIModel {
|
|||
|
||||
if (vnData.results.length !== 1) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Expected 1 result from query, got ${vnData.results.length}.`,
|
||||
userMessage: 'Unexpected VNDB response.',
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@ import { APIModel } from 'packages/obsidian/src/api/APIModel';
|
|||
import type MediaDbPlugin from 'packages/obsidian/src/main';
|
||||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import { WikiModel } from 'packages/obsidian/src/models/WikiModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind, toAppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { Logger } from 'packages/obsidian/src/utils/Logger';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind, toMdbError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MediaType } from 'packages/obsidian/src/utils/MediaType';
|
||||
import type { Result } from 'packages/obsidian/src/utils/result';
|
||||
import { err, fromPromise, ok } from 'packages/obsidian/src/utils/result';
|
||||
|
|
@ -53,7 +53,7 @@ export class WikipediaAPI extends APIModel {
|
|||
this.types = [MediaType.Wiki];
|
||||
}
|
||||
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], AppError>> {
|
||||
async searchByTitle(title: string): Promise<Result<MediaTypeModel[], MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by Title`);
|
||||
|
||||
const searchUrl = `https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=${encodeURIComponent(title)}&srlimit=20&utf8=&format=json&origin=*`;
|
||||
|
|
@ -66,7 +66,7 @@ export class WikipediaAPI extends APIModel {
|
|||
|
||||
if (fetchData.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: fetchData.status },
|
||||
|
|
@ -75,8 +75,8 @@ export class WikipediaAPI extends APIModel {
|
|||
|
||||
const response = fetchData as { status: number; json(): Promise<unknown> };
|
||||
const dataResult = await fromPromise(response.json(), cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Failed to parse response from ${this.apiName}`,
|
||||
userMessage: `Failed to parse response from ${this.apiName}`,
|
||||
context: { apiName: this.apiName },
|
||||
|
|
@ -105,7 +105,7 @@ export class WikipediaAPI extends APIModel {
|
|||
return ok(ret);
|
||||
}
|
||||
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, AppError>> {
|
||||
async getById(id: string): Promise<Result<MediaTypeModel, MDBError>> {
|
||||
Logger.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
|
||||
const searchUrl = `https://en.wikipedia.org/w/api.php?action=query&prop=info&pageids=${encodeURIComponent(id)}&inprop=url&format=json&origin=*`;
|
||||
|
|
@ -117,7 +117,7 @@ export class WikipediaAPI extends APIModel {
|
|||
|
||||
if (fetchData.status !== 200) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
userMessage: `Received status code ${fetchData.status} from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, status: fetchData.status, id },
|
||||
|
|
@ -126,8 +126,8 @@ export class WikipediaAPI extends APIModel {
|
|||
|
||||
const response = fetchData as { status: number; json(): Promise<unknown> };
|
||||
const dataResult = await fromPromise(response.json(), cause =>
|
||||
toAppError(cause, {
|
||||
kind: AppErrorKind.Network,
|
||||
toMdbError(cause, {
|
||||
kind: MDBErrorKind.Network,
|
||||
message: `MDB | Failed to parse response from ${this.apiName}`,
|
||||
userMessage: `Failed to parse response from ${this.apiName}`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
@ -141,7 +141,7 @@ export class WikipediaAPI extends APIModel {
|
|||
const result = Object.values(data?.query?.pages)[0];
|
||||
if (!result) {
|
||||
return err({
|
||||
kind: AppErrorKind.Api,
|
||||
kind: MDBErrorKind.Api,
|
||||
message: `MDB | No data received from ${this.apiName}.`,
|
||||
userMessage: `No data received from ${this.apiName}.`,
|
||||
context: { apiName: this.apiName, id },
|
||||
|
|
|
|||
|
|
@ -1,22 +1,22 @@
|
|||
import { Notice } from 'obsidian';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind } from 'packages/obsidian/src/utils/AppError';
|
||||
import { Logger } from 'packages/obsidian/src/utils/Logger';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind } from 'packages/obsidian/src/utils/MDBError';
|
||||
|
||||
export class ErrorReporter {
|
||||
notice(error: AppError): void {
|
||||
notice(error: MDBError): void {
|
||||
const message = error.userMessage ?? error.message;
|
||||
new Notice(message);
|
||||
}
|
||||
|
||||
log(error: AppError): void {
|
||||
log(error: MDBError): void {
|
||||
Logger.warn('MDB | error', error);
|
||||
}
|
||||
|
||||
report(error: AppError): void {
|
||||
report(error: MDBError): void {
|
||||
this.log(error);
|
||||
|
||||
if (error.kind !== AppErrorKind.Cancelled) {
|
||||
if (error.kind !== MDBErrorKind.Cancelled) {
|
||||
this.notice(error);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
export enum AppErrorKind {
|
||||
export enum MDBErrorKind {
|
||||
Validation = 'Validation',
|
||||
Api = 'Api',
|
||||
Network = 'Network',
|
||||
|
|
@ -8,17 +8,15 @@ export enum AppErrorKind {
|
|||
Unexpected = 'Unexpected',
|
||||
}
|
||||
|
||||
export interface AppError {
|
||||
kind: AppErrorKind;
|
||||
export interface MDBError {
|
||||
kind: MDBErrorKind;
|
||||
message: string;
|
||||
userMessage?: string;
|
||||
cause?: unknown;
|
||||
context?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export const appError = (error: AppError): AppError => error;
|
||||
|
||||
export const toAppError = (cause: unknown, fallback: Omit<AppError, 'cause'>): AppError => {
|
||||
export function toMdbError(cause: unknown, fallback: Omit<MDBError, 'cause'>): MDBError {
|
||||
const message = cause instanceof Error ? cause.message : String(cause);
|
||||
return { ...fallback, message: fallback.message || message, cause };
|
||||
};
|
||||
}
|
||||
|
|
@ -5,8 +5,8 @@ import type { SeasonSelectModalElement } from 'packages/obsidian/src/modals/Medi
|
|||
import { MediaDbSeasonSelectModal } from 'packages/obsidian/src/modals/MediaDbSeasonSelectModal';
|
||||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import type { SeasonModel } from 'packages/obsidian/src/models/SeasonModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind } from 'packages/obsidian/src/utils/AppError';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MediaType } from 'packages/obsidian/src/utils/MediaType';
|
||||
import type { SearchModalOptions } from 'packages/obsidian/src/utils/ModalHelper';
|
||||
|
||||
|
|
@ -17,7 +17,7 @@ export class MediaDbEntryHelper {
|
|||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
private reportAppError(error: AppError): void {
|
||||
private reportMdbError(error: MDBError): void {
|
||||
this.plugin.errorReporter.report(error);
|
||||
}
|
||||
|
||||
|
|
@ -29,12 +29,12 @@ export class MediaDbEntryHelper {
|
|||
|
||||
const apiSearchResults = await this.plugin.apiManager.query(advancedSearch.query, advancedSearch.apis);
|
||||
if (!apiSearchResults.ok) {
|
||||
this.reportAppError(apiSearchResults.error);
|
||||
this.reportMdbError(apiSearchResults.error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!apiSearchResults.value.items || apiSearchResults.value.items.length === 0) {
|
||||
this.reportAppError({ kind: AppErrorKind.Validation, message: 'No results found.', userMessage: 'No results found.' });
|
||||
this.reportMdbError({ kind: MDBErrorKind.Validation, message: 'No results found.', userMessage: 'No results found.' });
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -66,18 +66,18 @@ export class MediaDbEntryHelper {
|
|||
const apis = this.plugin.apiManager.apis.filter(api => api.hasTypeOverlap(types)).map(api => api.apiName);
|
||||
const apiSearchResults = await this.plugin.apiManager.query(searchData.query, apis);
|
||||
if (!apiSearchResults.ok) {
|
||||
this.reportAppError(apiSearchResults.error);
|
||||
this.reportMdbError(apiSearchResults.error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!apiSearchResults.value.items || apiSearchResults.value.items.length === 0) {
|
||||
this.reportAppError({ kind: AppErrorKind.Validation, message: 'No results found.', userMessage: 'No results found.' });
|
||||
this.reportMdbError({ kind: MDBErrorKind.Validation, message: 'No results found.', userMessage: 'No results found.' });
|
||||
return;
|
||||
}
|
||||
|
||||
const filteredSearchResults = apiSearchResults.value.items.filter(result => types.includes(result.getMediaType()));
|
||||
if (filteredSearchResults.length === 0) {
|
||||
this.reportAppError({ kind: AppErrorKind.Validation, message: 'No results found for the selected types.', userMessage: 'No results found for the selected types.' });
|
||||
this.reportMdbError({ kind: MDBErrorKind.Validation, message: 'No results found for the selected types.', userMessage: 'No results found for the selected types.' });
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -121,12 +121,12 @@ export class MediaDbEntryHelper {
|
|||
|
||||
const apiSearchResults = await this.plugin.apiManager.query(advancedSearch.query, advancedSearch.apis);
|
||||
if (!apiSearchResults.ok) {
|
||||
this.reportAppError(apiSearchResults.error);
|
||||
this.reportMdbError(apiSearchResults.error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!apiSearchResults.value.items || apiSearchResults.value.items.length === 0) {
|
||||
this.reportAppError({ kind: AppErrorKind.Validation, message: 'No results found.', userMessage: 'No results found.' });
|
||||
this.reportMdbError({ kind: MDBErrorKind.Validation, message: 'No results found.', userMessage: 'No results found.' });
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -160,7 +160,7 @@ export class MediaDbEntryHelper {
|
|||
|
||||
const queriedIdResult = await this.plugin.apiManager.queryDetailedInfoById(idSearchData.query, idSearchData.api);
|
||||
if (!queriedIdResult.ok) {
|
||||
this.reportAppError(queriedIdResult.error);
|
||||
this.reportMdbError(queriedIdResult.error);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -183,7 +183,7 @@ export class MediaDbEntryHelper {
|
|||
|
||||
const createNoteResult = await this.plugin.fileHelper.createMediaDbNoteFromModel(idSearchResult, { attachTemplate: true, openNote: true });
|
||||
if (!createNoteResult.ok) {
|
||||
this.reportAppError(createNoteResult.error);
|
||||
this.reportMdbError(createNoteResult.error);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -195,7 +195,7 @@ export class MediaDbEntryHelper {
|
|||
if (result.ok && result.value) {
|
||||
detailModels.push(result.value);
|
||||
} else if (!result.ok) {
|
||||
this.reportAppError(result.error);
|
||||
this.reportMdbError(result.error);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -228,7 +228,7 @@ export class MediaDbEntryHelper {
|
|||
|
||||
const allSeasonsResult = await tmdbSeasonAPI.getSeasonsForSeries(seriesId);
|
||||
if (!allSeasonsResult.ok) {
|
||||
this.reportAppError(allSeasonsResult.error);
|
||||
this.reportMdbError(allSeasonsResult.error);
|
||||
new Notice(`Error loading seasons: ${allSeasonsResult.error.userMessage}`);
|
||||
return false;
|
||||
}
|
||||
|
|
@ -276,7 +276,7 @@ export class MediaDbEntryHelper {
|
|||
if (seasonModel) {
|
||||
const fullMetadataResult = await tmdbSeasonAPI.getById(seasonModel.id);
|
||||
if (!fullMetadataResult.ok) {
|
||||
this.reportAppError(fullMetadataResult.error);
|
||||
this.reportMdbError(fullMetadataResult.error);
|
||||
new Notice(`Failed to load season ${selectedSeason.season_number}: ${fullMetadataResult.error.userMessage}`);
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,9 +4,9 @@ import { Notice, normalizePath, parseYaml, requestUrl, stringifyYaml } from 'obs
|
|||
import type MediaDbPlugin from 'packages/obsidian/src/main';
|
||||
import { ConfirmOverwriteModal } from 'packages/obsidian/src/modals/ConfirmOverwriteModal';
|
||||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind, toAppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { Logger } from 'packages/obsidian/src/utils/Logger';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind, toMdbError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import type { MediaType } from 'packages/obsidian/src/utils/MediaType';
|
||||
import type { Result } from 'packages/obsidian/src/utils/result';
|
||||
import { err, ok } from 'packages/obsidian/src/utils/result';
|
||||
|
|
@ -29,7 +29,7 @@ export class MediaDbFileHelper {
|
|||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
async createMediaDbNotes(models: MediaTypeModel[], attachFile?: TFile): Promise<Result<void, AppError>> {
|
||||
async createMediaDbNotes(models: MediaTypeModel[], attachFile?: TFile): Promise<Result<void, MDBError>> {
|
||||
const results = await Promise.all(models.map(model => this.createMediaDbNoteFromModel(model, { attachTemplate: true, attachFile })));
|
||||
|
||||
const failures = results.filter(result => !result.ok);
|
||||
|
|
@ -45,7 +45,7 @@ export class MediaDbFileHelper {
|
|||
return ok(undefined);
|
||||
}
|
||||
|
||||
async createMediaDbNoteFromModel(mediaTypeModel: MediaTypeModel, options: CreateNoteOptions): Promise<Result<void, AppError>> {
|
||||
async createMediaDbNoteFromModel(mediaTypeModel: MediaTypeModel, options: CreateNoteOptions): Promise<Result<void, MDBError>> {
|
||||
Logger.debug('MDB | creating new note');
|
||||
|
||||
options.openNote = this.plugin.settings.openNoteInNewTab;
|
||||
|
|
@ -58,7 +58,7 @@ export class MediaDbFileHelper {
|
|||
}
|
||||
|
||||
const fileContentResult = await this.attempt(() => this.generateMediaDbNoteContents(mediaTypeModel, options), {
|
||||
kind: AppErrorKind.Unexpected,
|
||||
kind: MDBErrorKind.Unexpected,
|
||||
message: 'Failed to generate note contents',
|
||||
userMessage: 'Failed to generate note contents',
|
||||
});
|
||||
|
|
@ -67,7 +67,7 @@ export class MediaDbFileHelper {
|
|||
}
|
||||
|
||||
const folderResult = await this.attempt(() => this.plugin.mediaTypeManager.getFolder(mediaTypeModel, this.plugin.app), {
|
||||
kind: AppErrorKind.Vault,
|
||||
kind: MDBErrorKind.Vault,
|
||||
message: 'Failed to determine note folder',
|
||||
userMessage: 'Failed to determine note folder',
|
||||
});
|
||||
|
|
@ -84,7 +84,7 @@ export class MediaDbFileHelper {
|
|||
|
||||
if (this.plugin.settings.enableTemplaterIntegration) {
|
||||
const templaterResult = await this.attempt(() => useTemplaterPluginInFile(this.plugin.app, targetFileResult.value), {
|
||||
kind: AppErrorKind.Unexpected,
|
||||
kind: MDBErrorKind.Unexpected,
|
||||
message: 'Failed to apply templater to the note',
|
||||
userMessage: 'Failed to apply templater to the note',
|
||||
});
|
||||
|
|
@ -240,11 +240,11 @@ export class MediaDbFileHelper {
|
|||
return structuredClone(metadata ?? {});
|
||||
}
|
||||
|
||||
async createNote(fileName: string, fileContent: string, options: CreateNoteOptions): Promise<Result<TFile, AppError>> {
|
||||
async createNote(fileName: string, fileContent: string, options: CreateNoteOptions): Promise<Result<TFile, MDBError>> {
|
||||
const folder = options.folder ?? this.plugin.app.vault.getAbstractFileByPath('/');
|
||||
|
||||
if (!folder || !(folder instanceof TFolder)) {
|
||||
return err({ kind: AppErrorKind.Validation, message: 'MDB | invalid folder', userMessage: 'MDB | invalid folder' });
|
||||
return err({ kind: MDBErrorKind.Validation, message: 'MDB | invalid folder', userMessage: 'MDB | invalid folder' });
|
||||
}
|
||||
|
||||
fileName = replaceIllegalFileNameCharactersInString(fileName);
|
||||
|
|
@ -257,7 +257,7 @@ export class MediaDbFileHelper {
|
|||
});
|
||||
|
||||
if (!shouldOverwrite) {
|
||||
return err({ kind: AppErrorKind.Cancelled, message: 'MDB | file creation cancelled by user', userMessage: 'MDB | file creation cancelled by user' });
|
||||
return err({ kind: MDBErrorKind.Cancelled, message: 'MDB | file creation cancelled by user', userMessage: 'MDB | file creation cancelled by user' });
|
||||
}
|
||||
|
||||
await this.plugin.app.fileManager.trashFile(file);
|
||||
|
|
@ -278,7 +278,7 @@ export class MediaDbFileHelper {
|
|||
return ok(targetFile);
|
||||
}
|
||||
|
||||
private async downloadImageForMediaModel(mediaTypeModel: MediaTypeModel): Promise<Result<void, AppError>> {
|
||||
private async downloadImageForMediaModel(mediaTypeModel: MediaTypeModel): Promise<Result<void, MDBError>> {
|
||||
if (mediaTypeModel.image && typeof mediaTypeModel.image === 'string' && mediaTypeModel.image.startsWith('http')) {
|
||||
const imageUrl = mediaTypeModel.image;
|
||||
const imageResult = await this.attempt(
|
||||
|
|
@ -299,7 +299,7 @@ export class MediaDbFileHelper {
|
|||
mediaTypeModel.image = `[[${imagePath}]]`;
|
||||
},
|
||||
{
|
||||
kind: AppErrorKind.Network,
|
||||
kind: MDBErrorKind.Network,
|
||||
message: 'MDB | Failed to download image',
|
||||
userMessage: 'Failed to download image',
|
||||
},
|
||||
|
|
@ -314,12 +314,12 @@ export class MediaDbFileHelper {
|
|||
return ok(undefined);
|
||||
}
|
||||
|
||||
private async attempt<T>(operation: () => Promise<T> | T, fallback: Omit<AppError, 'cause'>): Promise<Result<T, AppError>> {
|
||||
private async attempt<T>(operation: () => Promise<T> | T, fallback: Omit<MDBError, 'cause'>): Promise<Result<T, MDBError>> {
|
||||
return await Promise.resolve()
|
||||
.then(operation)
|
||||
.then(
|
||||
value => ok(value),
|
||||
cause => err(toAppError(cause, fallback)),
|
||||
cause => err(toMdbError(cause, fallback)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ import { MediaDbPreviewModal } from 'packages/obsidian/src/modals/MediaDbPreview
|
|||
import { MediaDbSearchModal } from 'packages/obsidian/src/modals/MediaDbSearchModal';
|
||||
import { MediaDbSearchResultModal } from 'packages/obsidian/src/modals/MediaDbSearchResultModal';
|
||||
import type { MediaTypeModel } from 'packages/obsidian/src/models/MediaTypeModel';
|
||||
import type { AppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import { AppErrorKind, toAppError } from 'packages/obsidian/src/utils/AppError';
|
||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import { MDBErrorKind, toMdbError } from 'packages/obsidian/src/utils/MDBError';
|
||||
import type { MediaType } from 'packages/obsidian/src/utils/MediaType';
|
||||
import type { Outcome } from 'packages/obsidian/src/utils/result';
|
||||
import { cancelled, failure, OutcomeStatus, skipped, success } from 'packages/obsidian/src/utils/result';
|
||||
|
|
@ -106,7 +106,7 @@ export const SELECTMODALOPTIONSDEFAULT: SelectModalOptions = {
|
|||
};
|
||||
|
||||
interface ModalCoreResult<T, TModal> {
|
||||
modalResult: Outcome<T, AppError>;
|
||||
modalResult: Outcome<T, MDBError>;
|
||||
modal: TModal;
|
||||
}
|
||||
|
||||
|
|
@ -119,10 +119,10 @@ export class ModalHelper {
|
|||
|
||||
private async openModalCore<TData, TModal extends { open(): void; close(): void }>(
|
||||
createModal: () => TModal,
|
||||
wireHandlers: (modal: TModal, resolve: (result: Outcome<TData, AppError>) => void) => void,
|
||||
wireHandlers: (modal: TModal, resolve: (result: Outcome<TData, MDBError>) => void) => void,
|
||||
): Promise<ModalCoreResult<TData, TModal>> {
|
||||
const modal = createModal();
|
||||
const modalResult = await new Promise<Outcome<TData, AppError>>(resolve => {
|
||||
const modalResult = await new Promise<Outcome<TData, MDBError>>(resolve => {
|
||||
wireHandlers(modal, resolve);
|
||||
modal.open();
|
||||
});
|
||||
|
|
@ -130,7 +130,7 @@ export class ModalHelper {
|
|||
return { modalResult, modal };
|
||||
}
|
||||
|
||||
private async resolveOutcome<T>(outcomePromise: Promise<Outcome<T, AppError>>): Promise<T | undefined> {
|
||||
private async resolveOutcome<T>(outcomePromise: Promise<Outcome<T, MDBError>>): Promise<T | undefined> {
|
||||
const outcome = await outcomePromise;
|
||||
|
||||
if (outcome.status === OutcomeStatus.Ok) {
|
||||
|
|
@ -144,14 +144,14 @@ export class ModalHelper {
|
|||
return undefined;
|
||||
}
|
||||
|
||||
async createSearchModalOutcome(searchModalOptions: SearchModalOptions): Promise<Outcome<SearchModalData, AppError>> {
|
||||
async createSearchModalOutcome(searchModalOptions: SearchModalOptions): Promise<Outcome<SearchModalData, MDBError>> {
|
||||
const { modalResult, modal } = await this.openModalCore<SearchModalData, MediaDbSearchModal>(
|
||||
() => new MediaDbSearchModal(this.plugin, searchModalOptions),
|
||||
(modal, resolve) => {
|
||||
modal.setSubmitCb(res => resolve(success(res)));
|
||||
modal.setCloseCb(err => {
|
||||
if (err) {
|
||||
resolve(failure(toAppError(err, { kind: AppErrorKind.Modal, message: 'Search modal closed with an error' })));
|
||||
resolve(failure(toMdbError(err, { kind: MDBErrorKind.Modal, message: 'Search modal closed with an error' })));
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -171,14 +171,14 @@ export class ModalHelper {
|
|||
return await this.resolveOutcome(this.createSearchModalOutcome(searchModalOptions));
|
||||
}
|
||||
|
||||
async createAdvancedSearchModalOutcome(advancedSearchModalOptions: AdvancedSearchModalOptions): Promise<Outcome<AdvancedSearchModalData, AppError>> {
|
||||
async createAdvancedSearchModalOutcome(advancedSearchModalOptions: AdvancedSearchModalOptions): Promise<Outcome<AdvancedSearchModalData, MDBError>> {
|
||||
const { modalResult, modal } = await this.openModalCore<AdvancedSearchModalData, MediaDbAdvancedSearchModal>(
|
||||
() => new MediaDbAdvancedSearchModal(this.plugin, advancedSearchModalOptions),
|
||||
(modal, resolve) => {
|
||||
modal.setSubmitCb(res => resolve(success(res)));
|
||||
modal.setCloseCb(err => {
|
||||
if (err) {
|
||||
resolve(failure(toAppError(err, { kind: AppErrorKind.Modal, message: 'Advanced search modal closed with an error' })));
|
||||
resolve(failure(toMdbError(err, { kind: MDBErrorKind.Modal, message: 'Advanced search modal closed with an error' })));
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -198,14 +198,14 @@ export class ModalHelper {
|
|||
return await this.resolveOutcome(this.createAdvancedSearchModalOutcome(advancedSearchModalOptions));
|
||||
}
|
||||
|
||||
async createIdSearchModalOutcome(idSearchModalOptions: IdSearchModalOptions): Promise<Outcome<IdSearchModalData, AppError>> {
|
||||
async createIdSearchModalOutcome(idSearchModalOptions: IdSearchModalOptions): Promise<Outcome<IdSearchModalData, MDBError>> {
|
||||
const { modalResult, modal } = await this.openModalCore<IdSearchModalData, MediaDbIdSearchModal>(
|
||||
() => new MediaDbIdSearchModal(this.plugin, idSearchModalOptions),
|
||||
(modal, resolve) => {
|
||||
modal.setSubmitCb(res => resolve(success(res)));
|
||||
modal.setCloseCb(err => {
|
||||
if (err) {
|
||||
resolve(failure(toAppError(err, { kind: AppErrorKind.Modal, message: 'Id search modal closed with an error' })));
|
||||
resolve(failure(toMdbError(err, { kind: MDBErrorKind.Modal, message: 'Id search modal closed with an error' })));
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -225,7 +225,7 @@ export class ModalHelper {
|
|||
return await this.resolveOutcome(this.createIdSearchModalOutcome(idSearchModalOptions));
|
||||
}
|
||||
|
||||
async createSelectModalOutcome(selectModalOptions: SelectModalOptions): Promise<Outcome<SelectModalData, AppError>> {
|
||||
async createSelectModalOutcome(selectModalOptions: SelectModalOptions): Promise<Outcome<SelectModalData, MDBError>> {
|
||||
const { modalResult, modal } = await this.openModalCore<SelectModalData, MediaDbSearchResultModal>(
|
||||
() => new MediaDbSearchResultModal(this.plugin, selectModalOptions),
|
||||
(modal, resolve) => {
|
||||
|
|
@ -233,7 +233,7 @@ export class ModalHelper {
|
|||
modal.setSkipCallback(() => resolve(skipped()));
|
||||
modal.setCloseCb(err => {
|
||||
if (err) {
|
||||
resolve(failure(toAppError(err, { kind: AppErrorKind.Modal, message: 'Select modal closed with an error' })));
|
||||
resolve(failure(toMdbError(err, { kind: MDBErrorKind.Modal, message: 'Select modal closed with an error' })));
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -253,14 +253,14 @@ export class ModalHelper {
|
|||
return await this.resolveOutcome(this.createSelectModalOutcome(selectModalOptions));
|
||||
}
|
||||
|
||||
async createPreviewModalOutcome(previewModalOptions: PreviewModalOptions): Promise<Outcome<PreviewModalData, AppError>> {
|
||||
async createPreviewModalOutcome(previewModalOptions: PreviewModalOptions): Promise<Outcome<PreviewModalData, MDBError>> {
|
||||
const { modalResult, modal } = await this.openModalCore<PreviewModalData, MediaDbPreviewModal>(
|
||||
() => new MediaDbPreviewModal(this.plugin, previewModalOptions),
|
||||
(modal, resolve) => {
|
||||
modal.setSubmitCb(res => resolve(success(res)));
|
||||
modal.setCloseCb(err => {
|
||||
if (err) {
|
||||
resolve(failure(toAppError(err, { kind: AppErrorKind.Modal, message: 'Preview modal closed with an error' })));
|
||||
resolve(failure(toMdbError(err, { kind: MDBErrorKind.Modal, message: 'Preview modal closed with an error' })));
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,30 +8,44 @@ export interface Err<E> {
|
|||
}
|
||||
export type Result<T, E> = Ok<T> | Err<E>;
|
||||
|
||||
export const ok = <T>(value: T): Ok<T> => ({ ok: true, value });
|
||||
export const err = <E>(error: E): Err<E> => ({ ok: false, error });
|
||||
export function ok<T>(value: T): Ok<T> {
|
||||
return { ok: true, value };
|
||||
}
|
||||
export function err<E>(error: E): Err<E> {
|
||||
return { ok: false, error };
|
||||
}
|
||||
|
||||
export const isOk = <T, E>(result: Result<T, E>): result is Ok<T> => result.ok;
|
||||
export const isErr = <T, E>(result: Result<T, E>): result is Err<E> => !result.ok;
|
||||
export function isOk<T, E>(result: Result<T, E>): result is Ok<T> {
|
||||
return result.ok;
|
||||
}
|
||||
export function isErr<T, E>(result: Result<T, E>): result is Err<E> {
|
||||
return !result.ok;
|
||||
}
|
||||
|
||||
export const mapResult = <T, E, U>(result: Result<T, E>, mapper: (value: T) => U): Result<U, E> => (result.ok ? ok(mapper(result.value)) : result);
|
||||
export function mapResult<T, E, U>(result: Result<T, E>, mapper: (value: T) => U): Result<U, E> {
|
||||
return result.ok ? ok(mapper(result.value)) : result;
|
||||
}
|
||||
|
||||
export const mapError = <T, E, F>(result: Result<T, E>, mapper: (error: E) => F): Result<T, F> => (result.ok ? result : err(mapper(result.error)));
|
||||
export function mapError<T, E, F>(result: Result<T, E>, mapper: (error: E) => F): Result<T, F> {
|
||||
return result.ok ? result : err(mapper(result.error));
|
||||
}
|
||||
|
||||
export const andThen = <T, E, U>(result: Result<T, E>, binder: (value: T) => Result<U, E>): Result<U, E> => (result.ok ? binder(result.value) : result);
|
||||
export function andThen<T, E, U>(result: Result<T, E>, binder: (value: T) => Result<U, E>): Result<U, E> {
|
||||
return result.ok ? binder(result.value) : result;
|
||||
}
|
||||
|
||||
export const tapError = <T, E>(result: Result<T, E>, sideEffect: (error: E) => void): Result<T, E> => {
|
||||
export function tapError<T, E>(result: Result<T, E>, sideEffect: (error: E) => void): Result<T, E> {
|
||||
if (!result.ok) sideEffect(result.error);
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
export const fromPromise = async <T, E>(promise: Promise<T>, onError: (cause: unknown) => E): Promise<Result<T, E>> => {
|
||||
export async function fromPromise<T, E>(promise: Promise<T>, onError: (cause: unknown) => E): Promise<Result<T, E>> {
|
||||
try {
|
||||
return ok(await promise);
|
||||
} catch (cause) {
|
||||
return err(onError(cause));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export enum OutcomeStatus {
|
||||
Ok = 'ok',
|
||||
|
|
@ -46,7 +60,15 @@ export type Outcome<T, E> =
|
|||
| { status: OutcomeStatus.Skipped }
|
||||
| { status: OutcomeStatus.Error; error: E };
|
||||
|
||||
export const cancelled = (): Outcome<never, never> => ({ status: OutcomeStatus.Cancelled });
|
||||
export const skipped = (): Outcome<never, never> => ({ status: OutcomeStatus.Skipped });
|
||||
export const success = <T>(data: T): Outcome<T, never> => ({ status: OutcomeStatus.Ok, data });
|
||||
export const failure = <E>(error: E): Outcome<never, E> => ({ status: OutcomeStatus.Error, error });
|
||||
export function cancelled(): Outcome<never, never> {
|
||||
return { status: OutcomeStatus.Cancelled };
|
||||
}
|
||||
export function skipped(): Outcome<never, never> {
|
||||
return { status: OutcomeStatus.Skipped };
|
||||
}
|
||||
export function success<T>(data: T): Outcome<T, never> {
|
||||
return { status: OutcomeStatus.Ok, data };
|
||||
}
|
||||
export function failure<E>(error: E): Outcome<never, E> {
|
||||
return { status: OutcomeStatus.Error, error };
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue