Skip to content

Commit d174ae8

Browse files
authored
fix[api]: use QvacErrorDecoderAudio with options object and replace generic Error throws (#1666)
1 parent b4af799 commit d174ae8

4 files changed

Lines changed: 43 additions & 6 deletions

File tree

packages/qvac-lib-decoder-audio/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const QvacResponse = require('@qvac/response')
44
const QvacLogger = require('@qvac/logging')
55
const ffmpeg = require('bare-ffmpeg')
66
const BaseInference = require('@qvac/infer-base/WeightsProvider/BaseInference')
7+
const { QvacErrorDecoderAudio, ERR_CODES } = require('./utils/error')
78

89
/**
910
* FFmpeg-based audio decoder (single-threaded)
@@ -106,7 +107,10 @@ class FFmpegDecoder extends BaseInference {
106107

107108
// Validate audio format
108109
if (!this.SUPPORTED_AUDIO_FORMATS[this.config.audioFormat]) {
109-
throw new Error(`Unsupported audio format: ${this.config.audioFormat}`)
110+
throw new QvacErrorDecoderAudio({
111+
code: ERR_CODES.UNSUPPORTED_AUDIO_FORMAT,
112+
adds: this.config.audioFormat
113+
})
110114
}
111115

112116
this.isLoaded = true
@@ -135,7 +139,7 @@ class FFmpegDecoder extends BaseInference {
135139
*/
136140
async run (audioStream) {
137141
if (!this.isLoaded) {
138-
throw new Error('Decoder not loaded. Call load() first.')
142+
throw new QvacErrorDecoderAudio({ code: ERR_CODES.DECODER_NOT_LOADED })
139143
}
140144

141145
this.logger.info('Starting new audio stream processing')
@@ -397,7 +401,10 @@ class FFmpegDecoder extends BaseInference {
397401

398402
const streamIndex = this.config.streamIndex || 0
399403
if (format.streams[streamIndex] === undefined) {
400-
throw new Error('Stream index out of bounds')
404+
throw new QvacErrorDecoderAudio({
405+
code: ERR_CODES.STREAM_INDEX_OUT_OF_BOUNDS,
406+
adds: streamIndex
407+
})
401408
}
402409

403410
// Process the stream and generate decoded output

packages/qvac-lib-decoder-audio/test/unit/StreamAccumulator.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const test = require('brittle')
44
const createStreamAccumulator = require('../../utils/createStreamAccumulator.js')
5-
const { QvacErrorDecoderAudio } = require('../../utils/error.js')
5+
const { QvacErrorDecoderAudio, ERR_CODES } = require('../../utils/error.js')
66

77
const TARGET_BUFFER_SIZE = 64000
88

@@ -88,6 +88,21 @@ test('validates buffer size configuration', (t) => {
8888
})
8989
}, QvacErrorDecoderAudio)
9090

91+
let captured
92+
try {
93+
createStreamAccumulator({
94+
onChunk: () => {},
95+
onFinish: () => {},
96+
targetBufferSize: 1000
97+
})
98+
} catch (err) {
99+
captured = err
100+
}
101+
t.ok(captured instanceof QvacErrorDecoderAudio)
102+
t.is(captured.code, ERR_CODES.BUFFER_SIZE_TOO_SMALL)
103+
t.is(captured.name, 'BUFFER_SIZE_TOO_SMALL')
104+
t.is(captured.message, 'Target buffer size is too small')
105+
91106
const accumulator = createStreamAccumulator({
92107
onChunk: () => {},
93108
onFinish: () => {},

packages/qvac-lib-decoder-audio/utils/createStreamAccumulator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function createStreamAccumulator ({
2222
targetBufferSize = TARGET_BUFFER_SIZE
2323
}) {
2424
if (targetBufferSize < TARGET_BUFFER_SIZE) {
25-
throw new QvacErrorDecoderAudio(ERR_CODES.BUFFER_SIZE_TOO_SMALL)
25+
throw new QvacErrorDecoderAudio({ code: ERR_CODES.BUFFER_SIZE_TOO_SMALL })
2626
}
2727

2828
let accumulator = Buffer.alloc(0)

packages/qvac-lib-decoder-audio/utils/error.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ const ERR_CODES = Object.freeze({
1414
FAILED_TO_APPEND: 11005,
1515
FAILED_TO_GET_STATUS: 11006,
1616
FAILED_TO_DESTROY: 11007,
17-
BUFFER_SIZE_TOO_SMALL: 11008
17+
BUFFER_SIZE_TOO_SMALL: 11008,
18+
UNSUPPORTED_AUDIO_FORMAT: 11009,
19+
DECODER_NOT_LOADED: 11010,
20+
STREAM_INDEX_OUT_OF_BOUNDS: 11011
1821
})
1922

2023
addCodes({
@@ -49,6 +52,18 @@ addCodes({
4952
[ERR_CODES.BUFFER_SIZE_TOO_SMALL]: {
5053
name: 'BUFFER_SIZE_TOO_SMALL',
5154
message: 'Target buffer size is too small'
55+
},
56+
[ERR_CODES.UNSUPPORTED_AUDIO_FORMAT]: {
57+
name: 'UNSUPPORTED_AUDIO_FORMAT',
58+
message: (format) => `Unsupported audio format: ${format}`
59+
},
60+
[ERR_CODES.DECODER_NOT_LOADED]: {
61+
name: 'DECODER_NOT_LOADED',
62+
message: 'Decoder not loaded. Call load() first.'
63+
},
64+
[ERR_CODES.STREAM_INDEX_OUT_OF_BOUNDS]: {
65+
name: 'STREAM_INDEX_OUT_OF_BOUNDS',
66+
message: (index) => `Stream index out of bounds: ${index}`
5267
}
5368
}, {
5469
name,

0 commit comments

Comments
 (0)