Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug when API key is missing #161

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/api/apis/MobyGamesAPI.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { APIModel } from '../APIModel';
import { Notice } from 'obsidian';
import { MediaTypeModel } from '../../models/MediaTypeModel';
import MediaDbPlugin from '../../main';
import { GameModel } from '../../models/GameModel';
Expand All @@ -22,7 +23,9 @@ export class MobyGamesAPI extends APIModel {
console.log(`MDB | api "${this.apiName}" queried by Title`);

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}`;
Expand Down Expand Up @@ -65,6 +68,7 @@ export class MobyGamesAPI extends APIModel {
console.log(`MDB | api "${this.apiName}" queried by ID`);

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

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

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}.`);
}

Expand Down
11 changes: 9 additions & 2 deletions src/api/apis/OMDbAPI.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { APIModel } from '../APIModel';
import { Notice } from 'obsidian';
import { MediaTypeModel } from '../../models/MediaTypeModel';
import { MovieModel } from '../../models/MovieModel';
import MediaDbPlugin from '../../main';
Expand Down Expand Up @@ -29,7 +30,9 @@ export class OMDbAPI extends APIModel {
console.log(`MDB | api "${this.apiName}" queried by Title`);

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}`;
Expand Down Expand Up @@ -107,23 +110,27 @@ export class OMDbAPI extends APIModel {
console.log(`MDB | api "${this.apiName}" queried by ID`);

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

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

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.`);
}
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}.`);
}

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

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

Expand Down Expand Up @@ -222,4 +229,4 @@ export class OMDbAPI extends APIModel {

return;
}
}
}