File tree Expand file tree Collapse file tree 2 files changed +93
-2
lines changed
Expand file tree Collapse file tree 2 files changed +93
-2
lines changed Original file line number Diff line number Diff line change @@ -87,3 +87,69 @@ no-change-test: "with-handlers with identifier handler not refactorable"
8787(with-handlers ([exn:fail? handler])
8888 (void))
8989--------------------
90+
91+
92+ test: "error with format string and arguments refactorable to raise-arguments-error "
93+ --------------------
94+ (define (foo low high)
95+ (unless (<= low high)
96+ (error 'foo "low should be less than high, ~a ~a " low high))
97+ (void))
98+ ====================
99+ (define (foo low high)
100+ (unless (<= low high)
101+ (raise-arguments-error 'foo "low should be less than high " "low " low "high " high))
102+ (void))
103+ --------------------
104+
105+
106+ test: "error with single argument refactorable to raise-arguments-error "
107+ --------------------
108+ (define (bar x)
109+ (when (negative? x)
110+ (error 'bar "x must be non-negative: ~a " x))
111+ (void))
112+ ====================
113+ (define (bar x)
114+ (when (negative? x)
115+ (raise-arguments-error 'bar "x must be non-negative " "x " x))
116+ (void))
117+ --------------------
118+
119+
120+ no-change-test: "error without format placeholders not refactorable "
121+ --------------------
122+ (define (baz)
123+ (error 'baz "something went wrong " ))
124+ --------------------
125+
126+
127+ no-change-test: "error with non-identifier arguments not refactorable "
128+ --------------------
129+ (define (qux x)
130+ (error 'qux "value is: ~a " (+ x 1 )))
131+ --------------------
132+
133+
134+ no-change-test: "error with mismatched placeholder count not refactorable "
135+ --------------------
136+ (define (mismatch x y)
137+ (error 'mismatch "values: ~a ~a ~a " x y))
138+ --------------------
139+
140+
141+ no-change-test: "error with more arguments than placeholders not refactorable "
142+ --------------------
143+ (define (extra x y)
144+ (error 'extra "value: ~a " x y))
145+ --------------------
146+
147+
148+ test: "error with ~a at end of string "
149+ --------------------
150+ (define (end-tilde x)
151+ (error 'end-tilde "value is ~a " x))
152+ ====================
153+ (define (end-tilde x)
154+ (raise-arguments-error 'end-tilde "value is " "x " x))
155+ --------------------
Original file line number Diff line number Diff line change 99 [exception-suggestions refactoring-suite?]))
1010
1111
12- (require resyntax/base
12+ (require racket/string
13+ resyntax/base
1314 resyntax/default-recommendations/private/literal-constant
1415 syntax/parse)
1516
3334 body ... ))
3435
3536
37+ (define-refactoring-rule error-to-raise-arguments-error
38+ #:description
39+ "Use `raise-arguments-error` instead of `error` for better error messages that follow Racket \
40+ conventions. "
41+ #:literals (error)
42+
43+ (error sym:expr message:str arg:id ...+)
44+
45+ #:do [(define message-str (syntax-e (attribute message)))
46+ (define args-list (attribute arg))]
47+ #:when (= (length (regexp-match* #rx"~a " message-str)) (length args-list))
48+ #:do [(define cleaned-message (string-replace message-str "~a " "" ))
49+ ;; Clean up extra spaces and trailing punctuation from placeholder removal
50+ (define cleaned-message-normalized
51+ (regexp-replace* #rx" + " cleaned-message " " ))]
52+ #:with new-message (regexp-replace #rx"[,;: ]+$ " cleaned-message-normalized "" )
53+ #:with (arg-str ... )
54+ (for/list ([arg-id (in-list args-list)])
55+ (symbol->string (syntax-e arg-id)))
56+
57+ (raise-arguments-error sym new-message (~@ arg-str arg) ... ))
58+
59+
3660(define-refactoring-suite exception-suggestions
37- #:rules (literal-exception-handler-to-lambda))
61+ #:rules (literal-exception-handler-to-lambda
62+ error-to-raise-arguments-error))
You can’t perform that action at this time.
0 commit comments