change `get-preference' to treat read errors the same as other bad content

That is, return '(), but also now log an error.
This commit is contained in:
Matthew Flatt 2011-09-27 19:09:16 -06:00
parent 2ae6d0c55f
commit 247a51d5bd
2 changed files with 27 additions and 15 deletions

View File

@ -354,23 +354,33 @@
#f))))))]) #f))))))])
(let ([prefs (with-pref-params (let ([prefs (with-pref-params
(lambda () (lambda ()
(if use-lock? (with-handlers ([exn:fail:read? (lambda (exn)
(call-with-preference-file-lock (log-error
'get-preference (format "error reading preferences: ~a"
'shared (exn-message exn)))
(lambda () null)])
(make-lock-file-name pref-file)) (if use-lock?
(lambda () (call-with-preference-file-lock
(with-input-from-file pref-file read)) 'get-preference
lock-there) 'shared
(with-input-from-file pref-file read))))]) (lambda ()
(make-lock-file-name pref-file))
(lambda ()
(with-input-from-file pref-file read))
lock-there)
(with-input-from-file pref-file read)))))])
;; Make sure file content had the right shape: ;; Make sure file content had the right shape:
(if (and (list? prefs) (if (and (list? prefs)
(andmap (lambda (x) (andmap (lambda (x)
(and (pair? x) (pair? (cdr x)) (null? (cddr x)))) (and (pair? x)
(symbol? (car x))
(pair? (cdr x))
(null? (cddr x))))
prefs)) prefs))
prefs prefs
null))))) (begin
(log-error "preference file content is not a list of symbol--value lists")
null))))))
(let* ([fn (path->complete-path (let* ([fn (path->complete-path
(or filename (or filename
(find-system-path 'pref-file)))] (find-system-path 'pref-file)))]

View File

@ -937,11 +937,13 @@ read an @elemref["old-prefs"]{old preferences file}, and then a
collection, instead. If none of those files exists, the preference set collection, instead. If none of those files exists, the preference set
is empty. is empty.
The preference file should contain a symbol-keyed association list The preference file should contain a list of symbol--value lists
(written to the file with the default parameter settings). Keys written with the default parameter settings. Keys
starting with @racket[racket:], @racket[mzscheme:], @racket[mred:], starting with @racket[racket:], @racket[mzscheme:], @racket[mred:],
and @racket[plt:] in any letter case are reserved for use by Racket and @racket[plt:] in any letter case are reserved for use by Racket
implementers. implementers. If the preference file does not contain a list
of symbol--value lists, an error is logged via @racket[log-error]
and @racket[failure-thunk] is called.
The result of @racket[get-preference] is the value associated with The result of @racket[get-preference] is the value associated with
@racket[name] if it exists in the association list, or the result of @racket[name] if it exists in the association list, or the result of