From 83f1d0d2ace1edf1f4218388d4a9cc8dab60078e Mon Sep 17 00:00:00 2001 From: Hans Dijkema Date: Tue, 7 Jul 2026 09:33:50 +0200 Subject: [PATCH] Better error handling in audio-player.rkt --- audio-player.rkt | 63 ++++++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/audio-player.rkt b/audio-player.rkt index 4caa7bf..49cbbb0 100644 --- a/audio-player.rkt +++ b/audio-player.rkt @@ -5,6 +5,7 @@ racket/async-channel racket/runtime-path racket/string + "audio-errors.rkt" uni-channel "audio-placed-player.rkt" "opusfile-decoder.rkt" @@ -76,18 +77,23 @@ (eq? (car retval) sym)) #f)) +(define-struct internal-audio-retval + (kind code info retval)) + (define (to-ret-value ret) (if (list? ret) (if (null? ret) - (error (format "audio-player: no return value in ~a" ret)) + (raise-audio-error 'audio-error "audio-player: no return value, empty list returned") (let ((r (car ret))) - (when (eq? r 'error) - (err-sound "Got an error: ~a" ret)) - (car ret))) - (begin - (when(eq? ret 'error) - (err-sound "Got an error: ~a" ret)) - ret))) + (if (eq? r 'error) + (let ((code (cadr ret)) + (msg (caddr ret))) + (err-sound "Got an error: ~a - ~a" code msg) + (make-internal-audio-retval 'error code msg 'error)) + (make-internal-audio-retval 'retval #f #f r)))) + (if (eq? ret 'error) + (make-internal-audio-retval 'error 'audio-error "An undefined error was raised in the audio-player" 'error) + (make-internal-audio-retval 'retval #f #f ret)))) (define (is-event? evt sym) (is-return? evt sym)) @@ -255,44 +261,49 @@ ) ) +(define-syntax ap-rpc + (syntax-rules () + ((_ handle cmd args ...) + (let ((rv ((audio-play-rpc handle) cmd args ...))) + (if (eq? (internal-audio-retval-kind rv) 'error) + (raise-audio-error (internal-audio-retval-code rv) (internal-audio-retval-info rv)) + (internal-audio-retval-retval rv)))) + ) + ) + (define/contract (audio-play! handle audio-file) (-> audio-play? path-string? number?) - (let ((result ((audio-play-rpc handle) 'open audio-file))) - (when (eq? result 'error) - (err-sound "audio-play!: error for file ~a" audio-file) - (error "Got an error from the placed audio player") - ) - (cadr result))) + (ap-rpc handle 'open audio-file)) (define/contract (audio-pause! handle paused) (-> audio-play? boolean? symbol?) - ((audio-play-rpc handle) 'pause paused)) + (ap-rpc handle 'pause paused)) (define/contract (audio-paused? handle) (-> audio-play? boolean?) - ((audio-play-rpc handle) 'paused)) + (ap-rpc handle 'paused)) (define/contract (audio-stop! handle) (-> audio-play? symbol?) - ((audio-play-rpc handle) 'stop)) + (ap-rpc handle 'stop)) (define/contract (audio-quit! handle) (-> audio-play? (or/c number? boolean? symbol?)) - (let ((r ((audio-play-rpc handle) 'quit))) + (let ((r (ap-rpc handle 'quit))) (set-audio-play-valid?! handle #f) r)) (define/contract (audio-seek! handle percentage) (-> audio-play? (max-percentage? 100) symbol?) - ((audio-play-rpc handle) 'seek percentage)) + (ap-rpc handle 'seek percentage)) (define/contract (audio-volume! handle percentage) (-> audio-play? percentage? symbol?) - ((audio-play-rpc handle) 'volume percentage)) + (ap-rpc handle 'volume percentage)) (define/contract (audio-volume handle) (-> audio-play? percentage?) - ((audio-play-rpc handle) 'get-volume)) + (ap-rpc handle 'get-volume)) (define/contract (audio-full-state handle) (-> audio-play? hash?) @@ -345,23 +356,23 @@ (-> audio-play? number? number? (or/c symbol? boolean?)) (let ((from (if (< min 1) 1 (if (> min 10) 10 min))) (until (if (< max min) (+ min 1) (if (> max 30) 30 max)))) - ((audio-play-rpc handle) 'buf-seconds from until))) + (ap-rpc handle 'buf-seconds from until))) (define/contract (audio-ao-buf-ms! handle ms) (-> audio-play? integer? (or/c integer? boolean?)) - ((audio-play-rpc handle) 'ao-buf-ms ms)) + (ap-rpc handle 'ao-buf-ms ms)) (define/contract (audio-ao-buf-ms handle) (-> audio-play? (or/c integer? boolean?)) - ((audio-play-rpc handle) 'ao-buf-ms)) + (ap-rpc handle 'ao-buf-ms)) (define/contract (audio-param! handle param value) (-> audio-play? symbol? any? any?) - ((audio-play-rpc handle) 'param! param value)) + (ap-rpc handle 'param! param value)) (define/contract (audio-param handle param) (-> audio-play? symbol? any?) - ((audio-play-rpc handle) 'param param)) + (ap-rpc handle 'param param)) (define/contract (audio-parameterize! handle) (-> audio-play? void?)