Skip to content

Commit 7ae05b0

Browse files
Merge pull request #161 from ltctceplrm/api-checl
Fixed bug when API key is missing
2 parents e7ea9ab + 2dc3790 commit 7ae05b0

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/api/apis/MobyGamesAPI.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { APIModel } from '../APIModel';
2+
import { Notice } from 'obsidian';
23
import { MediaTypeModel } from '../../models/MediaTypeModel';
34
import MediaDbPlugin from '../../main';
45
import { GameModel } from '../../models/GameModel';
@@ -22,7 +23,9 @@ export class MobyGamesAPI extends APIModel {
2223
console.log(`MDB | api "${this.apiName}" queried by Title`);
2324

2425
if (!this.plugin.settings.MobyGamesKey) {
25-
throw Error(`MDB | API key for ${this.apiName} missing.`);
26+
console.error(new Error(`MDB | API key for ${this.apiName} missing.`));
27+
new Notice(`MediaDB | API key for ${this.apiName} missing.`);
28+
return [];
2629
}
2730

2831
const searchUrl = `${this.apiUrl}/games?title=${encodeURIComponent(title)}&api_key=${this.plugin.settings.MobyGamesKey}`;
@@ -65,6 +68,7 @@ export class MobyGamesAPI extends APIModel {
6568
console.log(`MDB | api "${this.apiName}" queried by ID`);
6669

6770
if (!this.plugin.settings.MobyGamesKey) {
71+
new Notice(`MediaDB | API key for ${this.apiName} missing.`);
6872
throw Error(`MDB | API key for ${this.apiName} missing.`);
6973
}
7074

@@ -75,6 +79,7 @@ export class MobyGamesAPI extends APIModel {
7579
console.debug(fetchData);
7680

7781
if (fetchData.status !== 200) {
82+
new Notice(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
7883
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
7984
}
8085

src/api/apis/OMDbAPI.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { APIModel } from '../APIModel';
2+
import { Notice } from 'obsidian';
23
import { MediaTypeModel } from '../../models/MediaTypeModel';
34
import { MovieModel } from '../../models/MovieModel';
45
import MediaDbPlugin from '../../main';
@@ -29,7 +30,9 @@ export class OMDbAPI extends APIModel {
2930
console.log(`MDB | api "${this.apiName}" queried by Title`);
3031

3132
if (!this.plugin.settings.OMDbKey) {
32-
throw Error(`MDB | API key for ${this.apiName} missing.`);
33+
console.error(new Error(`MDB | API key for ${this.apiName} missing.`));
34+
new Notice(`MediaDB | API key for ${this.apiName} missing.`);
35+
return [];
3336
}
3437

3538
const searchUrl = `https://www.omdbapi.com/?s=${encodeURIComponent(title)}&apikey=${this.plugin.settings.OMDbKey}`;
@@ -107,23 +110,27 @@ export class OMDbAPI extends APIModel {
107110
console.log(`MDB | api "${this.apiName}" queried by ID`);
108111

109112
if (!this.plugin.settings.OMDbKey) {
113+
new Notice(`MediaDB | API key for ${this.apiName} missing.`);
110114
throw Error(`MDB | API key for ${this.apiName} missing.`);
111115
}
112116

113117
const searchUrl = `https://www.omdbapi.com/?i=${encodeURIComponent(id)}&apikey=${this.plugin.settings.OMDbKey}`;
114118
const fetchData = await fetch(searchUrl);
115119

116120
if (fetchData.status === 401) {
121+
new Notice(`MDB | Authentication for ${this.apiName} failed. Check the API key.`);
117122
throw Error(`MDB | Authentication for ${this.apiName} failed. Check the API key.`);
118123
}
119124
if (fetchData.status !== 200) {
125+
new Notice(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
120126
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
121127
}
122128

123129
const result = await fetchData.json();
124130
// console.debug(result);
125131

126132
if (result.Response === 'False') {
133+
new Notice(`MDB | Received error from ${this.apiName}: ${result.Error}`);
127134
throw Error(`MDB | Received error from ${this.apiName}: ${result.Error}`);
128135
}
129136

@@ -222,4 +229,4 @@ export class OMDbAPI extends APIModel {
222229

223230
return;
224231
}
225-
}
232+
}

0 commit comments

Comments
 (0)