From f881673b320634788d8b20e91e3ba71356a77d2f Mon Sep 17 00:00:00 2001 From: Hans Dijkema Date: Tue, 7 Jul 2026 00:44:50 +0200 Subject: [PATCH] Audio error handling added - first basics, need to be enhanced --- audio-decoder.rkt | 16 +++++++++--- audio-errors.rkt | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 audio-errors.rkt diff --git a/audio-decoder.rkt b/audio-decoder.rkt index afef104..dca41ab 100644 --- a/audio-decoder.rkt +++ b/audio-decoder.rkt @@ -6,6 +6,7 @@ "ffmpeg-decoder.rkt" "audio-sniffer.rkt" "private/utils.rkt" + "audio-errors.rkt" racket/contract racket/string racket/path @@ -193,13 +194,20 @@ (let ((file (if (path? audio-file) (path->string audio-file) audio-file))) - (unless (audio-file-valid? audio-file) - (error (format "Not a valid audio file '~a'" audio-file))) + (unless (file-exists? audio-file) - (error (format "File '~a' does not exist" audio-file))) + (raise-audio-error 'file-not-found + "File not found: '~a'" audio-file)) + + (unless (audio-file-valid? audio-file) + (raise-audio-error 'invalid-audio-file + "Not a valid audio file: '~a'" audio-file)) + (let ((reader* (find-reader audio-file))) (when (eq? reader* #f) - (error (format "Cannot find reader for '~a'" audio-file))) + (raise-audio-error 'no-audio-reader + "Cannot find reader for '~a'" audio-file)) + (let* ((reader-type (car reader*)) (reader (cadr reader*)) (ao-type (audio-reader-ao-type reader)) diff --git a/audio-errors.rkt b/audio-errors.rkt new file mode 100644 index 0000000..d677a9c --- /dev/null +++ b/audio-errors.rkt @@ -0,0 +1,66 @@ +#lang racket/base + +(require (for-syntax racket/base) + racket/path) + + +(provide exn:fail:audio + exn:fail:audio? + exn:fail:audio-code + raise-audio-error + make-audio-error-code + exn-audio-code + ) + +(struct exn:fail:audio exn:fail + (code source line) + #:transparent) + +(define exn-audio-code exn:fail:audio-code) + +(define (audio-codes) + '(no-decoder no-encoder + file-not-found + invalid-audio-file + no-audio-reader + audio-file-corrupt + audio-error)) + +(define (make-audio-code code) + (if (symbol? code) + (letrec ((f (λ (l) + (if (null? (cdr l)) + (car l) + (if (eq? (car l) code) + code + (f (cdr l))))))) + (f (audio-codes))) + (make-audio-code (string->symbol (format "~a" code))))) + +(define (make-audio-error-code c) + (make-audio-code c)) + + +(define (raise-audio-error* code source line message . args) + (let* ((msg (apply format (cons message args))) + (src* (file-name-from-path (build-path source))) + (msg* (format "~a at ~a, line ~a" msg src* line))) + (raise (exn:fail:audio msg* + (current-continuation-marks) + (make-audio-code code) + source + line)))) + +(define-syntax (raise-audio-error stx) + (syntax-case stx () + ((_ code message ...) + (let ((src (syntax-source stx)) + (line (syntax-line stx))) + #`(raise-audio-error* code '#,src '#,line message ...))))) + +(define (test x) + (if (> x 10) + (raise-audio-error 'file-notd-found "File not found: ~a" "c:/muziek/a.flac") + #t)) + +