-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from suzuki3jp/develop
Develop
- Loading branch information
Showing
13 changed files
with
382 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: CI | ||
on: | ||
pull_request: | ||
types: [opened, reopened, synchronize, ready_for_review] | ||
workflow_dispatch: | ||
|
||
jobs: | ||
vitest: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
- name: Setup Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: "lts/*" | ||
- name: Install dependencies | ||
run: npm ci | ||
- name: Test | ||
run: npm run test | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
- name: Setup Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: "lts/*" | ||
- name: Install dependencies | ||
run: npm ci | ||
- name: Build | ||
run: npm run build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { GaxiosError } from "gaxios"; | ||
|
||
export type YouTubesJsErrors = LikelyBugError | YouTubeApiError; | ||
|
||
/** | ||
* Represents an error that is likely a bug in the library. | ||
* | ||
* If you encounter this error, please report the issue on [GitHub Issue](https://github.com/suzuki3jp/youtubes.js/issues/new). | ||
*/ | ||
export class LikelyBugError implements BaseError { | ||
public type = "LIKELY_BUG"; | ||
public message: string; | ||
|
||
constructor(message: string) { | ||
this.message = message; | ||
} | ||
|
||
/** | ||
* Converts the error to an `Error` object. | ||
* @returns | ||
*/ | ||
public toError(): Error { | ||
const error = new Error(this.message); | ||
error.name = "LikelyBugError"; | ||
return error; | ||
} | ||
|
||
/** | ||
* Throws the error. | ||
*/ | ||
public throw(): never { | ||
throw this.toError(); | ||
} | ||
} | ||
|
||
/** | ||
* Represents an error from the YouTube API. | ||
* | ||
* This error is thrown when the YouTube API returns an error response. | ||
*/ | ||
export class YouTubeApiError implements BaseError { | ||
public type = "YOUTUBE_API_ERROR"; | ||
public code: number; | ||
public message: string; | ||
|
||
constructor(code: number, message: string) { | ||
this.code = code; | ||
this.message = message; | ||
} | ||
|
||
/** | ||
* Converts the error to an `Error` object. | ||
* @returns | ||
*/ | ||
public toError(): Error { | ||
const error = new Error(`[${this.code}] ${this.message}`); | ||
error.name = "YouTubeApiError"; | ||
return error; | ||
} | ||
|
||
/** | ||
* Throws the error. | ||
*/ | ||
public throw(): never { | ||
throw this.toError(); | ||
} | ||
} | ||
|
||
interface BaseError { | ||
type: string; | ||
toError(): Error; | ||
} | ||
|
||
/** | ||
* Handles an error from the YouTube API cathing in the try-catch block. | ||
* @param error | ||
*/ | ||
export function handleYouTubeApiError(error: unknown): YouTubesJsErrors { | ||
if (error instanceof GaxiosError) { | ||
const code = Number.parseInt(error.code || "500"); | ||
return new YouTubeApiError(code, error.message); | ||
} | ||
|
||
return new LikelyBugError( | ||
"An unexpected error occurred in the call to the YouTube API.", | ||
); | ||
} |
Oops, something went wrong.