Merge pull request #161 from ltctceplrm/api-checl

Fixed bug when API key is missing
This commit is contained in:
Moritz Jung 2025-01-14 21:07:30 +01:00 committed by GitHub
commit 7ae05b03c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 3 deletions

View file

@ -1,4 +1,5 @@
import { APIModel } from '../APIModel'; import { APIModel } from '../APIModel';
import { Notice } from 'obsidian';
import { MediaTypeModel } from '../../models/MediaTypeModel'; import { MediaTypeModel } from '../../models/MediaTypeModel';
import MediaDbPlugin from '../../main'; import MediaDbPlugin from '../../main';
import { GameModel } from '../../models/GameModel'; import { GameModel } from '../../models/GameModel';
@ -22,7 +23,9 @@ export class MobyGamesAPI extends APIModel {
console.log(`MDB | api "${this.apiName}" queried by Title`); console.log(`MDB | api "${this.apiName}" queried by Title`);
if (!this.plugin.settings.MobyGamesKey) { if (!this.plugin.settings.MobyGamesKey) {
throw Error(`MDB | API key for ${this.apiName} missing.`); console.error(new Error(`MDB | API key for ${this.apiName} missing.`));
new Notice(`MediaDB | API key for ${this.apiName} missing.`);
return [];
} }
const searchUrl = `${this.apiUrl}/games?title=${encodeURIComponent(title)}&api_key=${this.plugin.settings.MobyGamesKey}`; const searchUrl = `${this.apiUrl}/games?title=${encodeURIComponent(title)}&api_key=${this.plugin.settings.MobyGamesKey}`;
@ -65,6 +68,7 @@ export class MobyGamesAPI extends APIModel {
console.log(`MDB | api "${this.apiName}" queried by ID`); console.log(`MDB | api "${this.apiName}" queried by ID`);
if (!this.plugin.settings.MobyGamesKey) { if (!this.plugin.settings.MobyGamesKey) {
new Notice(`MediaDB | API key for ${this.apiName} missing.`);
throw Error(`MDB | API key for ${this.apiName} missing.`); throw Error(`MDB | API key for ${this.apiName} missing.`);
} }
@ -75,6 +79,7 @@ export class MobyGamesAPI extends APIModel {
console.debug(fetchData); console.debug(fetchData);
if (fetchData.status !== 200) { if (fetchData.status !== 200) {
new Notice(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`); throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
} }

View file

@ -1,4 +1,5 @@
import { APIModel } from '../APIModel'; import { APIModel } from '../APIModel';
import { Notice } from 'obsidian';
import { MediaTypeModel } from '../../models/MediaTypeModel'; import { MediaTypeModel } from '../../models/MediaTypeModel';
import { MovieModel } from '../../models/MovieModel'; import { MovieModel } from '../../models/MovieModel';
import MediaDbPlugin from '../../main'; import MediaDbPlugin from '../../main';
@ -29,7 +30,9 @@ export class OMDbAPI extends APIModel {
console.log(`MDB | api "${this.apiName}" queried by Title`); console.log(`MDB | api "${this.apiName}" queried by Title`);
if (!this.plugin.settings.OMDbKey) { if (!this.plugin.settings.OMDbKey) {
throw Error(`MDB | API key for ${this.apiName} missing.`); console.error(new Error(`MDB | API key for ${this.apiName} missing.`));
new Notice(`MediaDB | API key for ${this.apiName} missing.`);
return [];
} }
const searchUrl = `https://www.omdbapi.com/?s=${encodeURIComponent(title)}&apikey=${this.plugin.settings.OMDbKey}`; const searchUrl = `https://www.omdbapi.com/?s=${encodeURIComponent(title)}&apikey=${this.plugin.settings.OMDbKey}`;
@ -107,6 +110,7 @@ export class OMDbAPI extends APIModel {
console.log(`MDB | api "${this.apiName}" queried by ID`); console.log(`MDB | api "${this.apiName}" queried by ID`);
if (!this.plugin.settings.OMDbKey) { if (!this.plugin.settings.OMDbKey) {
new Notice(`MediaDB | API key for ${this.apiName} missing.`);
throw Error(`MDB | API key for ${this.apiName} missing.`); throw Error(`MDB | API key for ${this.apiName} missing.`);
} }
@ -114,9 +118,11 @@ export class OMDbAPI extends APIModel {
const fetchData = await fetch(searchUrl); const fetchData = await fetch(searchUrl);
if (fetchData.status === 401) { if (fetchData.status === 401) {
new Notice(`MDB | Authentication for ${this.apiName} failed. Check the API key.`);
throw Error(`MDB | Authentication for ${this.apiName} failed. Check the API key.`); throw Error(`MDB | Authentication for ${this.apiName} failed. Check the API key.`);
} }
if (fetchData.status !== 200) { if (fetchData.status !== 200) {
new Notice(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`); throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
} }
@ -124,6 +130,7 @@ export class OMDbAPI extends APIModel {
// console.debug(result); // console.debug(result);
if (result.Response === 'False') { if (result.Response === 'False') {
new Notice(`MDB | Received error from ${this.apiName}: ${result.Error}`);
throw Error(`MDB | Received error from ${this.apiName}: ${result.Error}`); throw Error(`MDB | Received error from ${this.apiName}: ${result.Error}`);
} }
@ -222,4 +229,4 @@ export class OMDbAPI extends APIModel {
return; return;
} }
} }