@@ -4,32 +4,28 @@ defmodule IdempotencyPlug do
4
4
There's no Idempotency-Key request headers.
5
5
"""
6
6
7
- defexception [
8
- message: "No idempotency key found. You need to set the `Idempotency-Key` header for all POST requests: 'Idempotency-Key: KEY'" ,
9
- plug_status: :bad_request
10
- ]
7
+ defexception message:
8
+ "No idempotency key found. You need to set the `Idempotency-Key` header for all POST requests: 'Idempotency-Key: KEY'" ,
9
+ plug_status: :bad_request
11
10
end
12
11
13
12
defmodule MultipleHeadersError do
14
13
@ moduledoc """
15
14
There are multiple Idempotency-Key request headers.
16
15
"""
17
16
18
- defexception [
19
- message: "Only one `Idempotency-Key` header can be sent" ,
20
- plug_status: :bad_request
21
- ]
17
+ defexception message: "Only one `Idempotency-Key` header can be sent" ,
18
+ plug_status: :bad_request
22
19
end
23
20
24
21
defmodule ConcurrentRequestError do
25
22
@ moduledoc """
26
23
There's another request currently being processed for this ID.
27
24
"""
28
25
29
- defexception [
30
- message: "A request with the same `Idempotency-Key` is currently being processed" ,
31
- plug_status: :conflict
32
- ]
26
+ defexception message:
27
+ "A request with the same `Idempotency-Key` is currently being processed" ,
28
+ plug_status: :conflict
33
29
end
34
30
35
31
defmodule RequestPayloadFingerprintMismatchError do
@@ -51,18 +47,20 @@ defmodule IdempotencyPlug do
51
47
52
48
defexception [
53
49
:reason ,
54
- message: "The original request was interrupted and can't be recovered as it's in an unknown state" ,
50
+ message:
51
+ "The original request was interrupted and can't be recovered as it's in an unknown state" ,
55
52
plug_status: :internal_server_error
56
53
]
57
54
end
58
55
59
- defimpl Plug.Exception , for: [
60
- NoHeadersError ,
61
- MultipleHeadersError ,
62
- ConcurrentRequestError ,
63
- RequestPayloadFingerprintMismatchError ,
64
- HaltedResponseError
65
- ] do
56
+ defimpl Plug.Exception ,
57
+ for: [
58
+ NoHeadersError ,
59
+ MultipleHeadersError ,
60
+ ConcurrentRequestError ,
61
+ RequestPayloadFingerprintMismatchError ,
62
+ HaltedResponseError
63
+ ] do
66
64
def status ( % { plug_status: status } ) , do: Plug.Conn.Status . code ( status )
67
65
def actions ( _ ) , do: [ ]
68
66
end
@@ -173,7 +171,7 @@ defmodule IdempotencyPlug do
173
171
174
172
other ->
175
173
raise ArgumentError ,
176
- "option :tracker must be one of PID or Atom, got: #{ inspect ( other ) } "
174
+ "option :tracker must be one of PID or Atom, got: #{ inspect ( other ) } "
177
175
end
178
176
179
177
opts
@@ -208,7 +206,7 @@ defmodule IdempotencyPlug do
208
206
other ->
209
207
# credo:disable-for-next-line Credo.Check.Warning.RaiseInsideRescue
210
208
raise ArgumentError ,
211
- "option :with should be one of :exception or MFA, got: #{ inspect ( other ) } "
209
+ "option :with should be one of :exception or MFA, got: #{ inspect ( other ) } "
212
210
end
213
211
end
214
212
@@ -254,9 +252,14 @@ defmodule IdempotencyPlug do
254
252
255
253
processed_key =
256
254
case Keyword . get ( opts , :idempotency_key , { __MODULE__ , :idempotency_key } ) do
257
- { mod , fun } -> apply ( mod , fun , [ conn , key ] )
258
- { mod , fun , args } -> apply ( mod , fun , [ conn , key | args ] )
259
- other -> raise ArgumentError , "option :idempotency_key must be a MFA, got: #{ inspect ( other ) } "
255
+ { mod , fun } ->
256
+ apply ( mod , fun , [ conn , key ] )
257
+
258
+ { mod , fun , args } ->
259
+ apply ( mod , fun , [ conn , key | args ] )
260
+
261
+ other ->
262
+ raise ArgumentError , "option :idempotency_key must be a MFA, got: #{ inspect ( other ) } "
260
263
end
261
264
262
265
hash ( :idempotency_key , processed_key , opts )
@@ -275,9 +278,15 @@ defmodule IdempotencyPlug do
275
278
defp hash_request_payload ( conn , opts ) do
276
279
payload =
277
280
case Keyword . get ( opts , :request_payload , { __MODULE__ , :request_payload } ) do
278
- { mod , fun } -> apply ( mod , fun , [ conn ] )
279
- { mod , fun , args } -> apply ( mod , fun , [ conn | args ] )
280
- other -> raise ArgumentError , "option :request_payload must be a MFA tuple, got: #{ inspect ( other ) } "
281
+ { mod , fun } ->
282
+ apply ( mod , fun , [ conn ] )
283
+
284
+ { mod , fun , args } ->
285
+ apply ( mod , fun , [ conn | args ] )
286
+
287
+ other ->
288
+ raise ArgumentError ,
289
+ "option :request_payload must be a MFA tuple, got: #{ inspect ( other ) } "
281
290
end
282
291
283
292
hash ( :request_payload , payload , opts )
@@ -308,7 +317,7 @@ defmodule IdempotencyPlug do
308
317
Conn . register_before_send ( conn , fn conn ->
309
318
case RequestTracker . put_response ( tracker , key , conn_to_response ( conn ) ) do
310
319
{ :ok , expires } -> put_expires_header ( conn , expires )
311
- { :error , error } -> raise "failed to put response in cache store, got: #{ inspect error } "
320
+ { :error , error } -> raise "failed to put response in cache store, got: #{ inspect ( error ) } "
312
321
end
313
322
end )
314
323
end
@@ -338,8 +347,11 @@ defmodule IdempotencyPlug do
338
347
mod
339
348
|> apply ( fun , [ conn , error | args ] )
340
349
|> case do
341
- % Conn { halted: true } = conn -> conn
342
- other -> raise ArgumentError , "option :with MUST return a halted conn, got: #{ inspect ( other ) } "
350
+ % Conn { halted: true } = conn ->
351
+ conn
352
+
353
+ other ->
354
+ raise ArgumentError , "option :with MUST return a halted conn, got: #{ inspect ( other ) } "
343
355
end
344
356
end
345
357
end
0 commit comments