Repository navigation
fix(bot): expose degraded-extractor signal and honest play error #1999
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
db91c4d
fix(bot): expose degraded-extractor signal and honest play error (#1929)
LucasSantana-Dev daba978
Merge branch 'main' into fix/1929-extractor-degraded-signal
LucasSantana-Dev 37bc364
Merge branch 'main' into fix/1929-extractor-degraded-signal
LucasSantana-Dev ed310eb
Merge branch 'main' into fix/1929-extractor-degraded-signal
LucasSantana-Dev e34c2cd
Merge branch 'main' into fix/1929-extractor-degraded-signal
LucasSantana-Dev 0c8b9bf
Merge branch 'main' into fix/1929-extractor-degraded-signal
LucasSantana-Dev bb508f7
Merge branch 'main' into fix/1929-extractor-degraded-signal
LucasSantana-Dev 67bdd14
Merge branch 'main' into fix/1929-extractor-degraded-signal
LucasSantana-Dev db1b958
Merge branch 'main' into fix/1929-extractor-degraded-signal
LucasSantana-Dev f4659c5
Merge branch 'main' into fix/1929-extractor-degraded-signal
LucasSantana-Dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
packages/bot/src/functions/music/commands/play/handlers/playErrorMessage.spec.ts
This file contains hidden or 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,53 @@ | ||
| import { beforeEach, describe, expect, it, jest } from '@jest/globals' | ||
|
|
||
| const isExtractorDegradedMock = jest.fn(() => false) | ||
|
|
||
| jest.mock('../../../../../handlers/player/extractorHealth', () => ({ | ||
| isExtractorDegraded: (...args: unknown[]) => | ||
| isExtractorDegradedMock(...args), | ||
| })) | ||
|
|
||
| import { resolvePlayErrorMessage } from './playErrorMessage' | ||
|
|
||
| describe('resolvePlayErrorMessage', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| isExtractorDegradedMock.mockReturnValue(false) | ||
| }) | ||
|
|
||
| it('reports sources unreachable for a no-results error while youtube is degraded', () => { | ||
| isExtractorDegradedMock.mockReturnValue(true) | ||
| const error = new Error( | ||
| 'No results found for "some song" (Extractor: com.discord-player.youtube)', | ||
| ) | ||
|
|
||
| expect(resolvePlayErrorMessage(error)).toBe( | ||
| 'Music sources are currently unreachable. Please try again in a few minutes.', | ||
| ) | ||
| expect(isExtractorDegradedMock).toHaveBeenCalledWith('youtube') | ||
| }) | ||
|
|
||
| it('falls back to the sanitized message for a no-results error when youtube is healthy', () => { | ||
| isExtractorDegradedMock.mockReturnValue(false) | ||
| const error = new Error('No results found for "some song"') | ||
|
|
||
| expect(resolvePlayErrorMessage(error)).toContain('No results found') | ||
| }) | ||
|
|
||
| it('does not override an unrelated error even while youtube is degraded', () => { | ||
| isExtractorDegradedMock.mockReturnValue(true) | ||
| const error = new Error('ffmpeg exited unexpectedly') | ||
|
|
||
| expect(resolvePlayErrorMessage(error)).not.toBe( | ||
| 'Music sources are currently unreachable. Please try again in a few minutes.', | ||
| ) | ||
| }) | ||
|
|
||
| it('handles a non-Error thrown value without throwing', () => { | ||
| isExtractorDegradedMock.mockReturnValue(true) | ||
|
|
||
| expect(() => | ||
| resolvePlayErrorMessage('plain string error'), | ||
| ).not.toThrow() | ||
| }) | ||
| }) |
14 changes: 14 additions & 0 deletions
14
packages/bot/src/functions/music/commands/play/handlers/playErrorMessage.ts
This file contains hidden or 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,14 @@ | ||
| import { createUserFriendlyError } from '@lucky/shared/utils/general/errorSanitizer' | ||
| import { isExtractorDegraded } from '../../../../../handlers/player/extractorHealth' | ||
|
|
||
| // "No results found" is misleading when it's actually a degraded extractor | ||
| // (e.g. YouTube session init silently failed but registration still | ||
| // reported success) — say so instead (#1929). | ||
| export function resolvePlayErrorMessage(error: unknown): string { | ||
| const isNoResultsError = | ||
| error instanceof Error && /no results found/i.test(error.message) | ||
| if (isNoResultsError && isExtractorDegraded('youtube')) { | ||
|
LucasSantana-Dev marked this conversation as resolved.
|
||
| return 'Music sources are currently unreachable. Please try again in a few minutes.' | ||
| } | ||
| return createUserFriendlyError(error) | ||
| } | ||
This file contains hidden or 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 hidden or 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,48 @@ | ||
| import { beforeEach, describe, expect, it, jest } from '@jest/globals' | ||
|
|
||
| const gaugeSetMock = jest.fn() | ||
|
|
||
| jest.mock('../../utils/monitoring/prometheus', () => ({ | ||
| musicExtractorDegradedGauge: { | ||
| set: (...args: unknown[]) => gaugeSetMock(...args), | ||
| }, | ||
| })) | ||
|
|
||
| import { isExtractorDegraded, setExtractorDegraded } from './extractorHealth' | ||
|
|
||
| describe('extractorHealth', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| setExtractorDegraded('youtube', false) | ||
| jest.clearAllMocks() | ||
| }) | ||
|
|
||
| it('reports healthy by default', () => { | ||
| expect(isExtractorDegraded('youtube')).toBe(false) | ||
| }) | ||
|
|
||
| it('marks an extractor degraded and updates the gauge', () => { | ||
| setExtractorDegraded('youtube', true) | ||
|
|
||
| expect(isExtractorDegraded('youtube')).toBe(true) | ||
| expect(gaugeSetMock).toHaveBeenCalledWith({ extractor: 'youtube' }, 1) | ||
| }) | ||
|
|
||
| it('clears a degraded extractor and updates the gauge', () => { | ||
| setExtractorDegraded('youtube', true) | ||
| setExtractorDegraded('youtube', false) | ||
|
|
||
| expect(isExtractorDegraded('youtube')).toBe(false) | ||
| expect(gaugeSetMock).toHaveBeenLastCalledWith( | ||
| { extractor: 'youtube' }, | ||
| 0, | ||
| ) | ||
| }) | ||
|
|
||
| it('tracks extractors independently', () => { | ||
| setExtractorDegraded('youtube', true) | ||
|
|
||
| expect(isExtractorDegraded('youtube')).toBe(true) | ||
| expect(isExtractorDegraded('spotify')).toBe(false) | ||
| }) | ||
| }) |
This file contains hidden or 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,24 @@ | ||
| import { musicExtractorDegradedGauge } from '../../utils/monitoring/prometheus' | ||
|
|
||
| const degradedExtractors = new Set<string>() | ||
|
|
||
| /** | ||
| * Single source of truth for extractor health: updates both the in-process | ||
| * readable state (checked at /play error time, #1929) and the Prometheus | ||
| * gauge (scraped for alerting) together, so they can't drift apart. | ||
| */ | ||
| export function setExtractorDegraded( | ||
| extractor: string, | ||
| degraded: boolean, | ||
| ): void { | ||
| if (degraded) { | ||
| degradedExtractors.add(extractor) | ||
| } else { | ||
| degradedExtractors.delete(extractor) | ||
| } | ||
| musicExtractorDegradedGauge.set({ extractor }, degraded ? 1 : 0) | ||
| } | ||
|
|
||
| export function isExtractorDegraded(extractor: string): boolean { | ||
| return degradedExtractors.has(extractor) | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.