wakio 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +17 -1
  3. data/lib/wakio/delivery_method.rb +24 -11
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0b10c47e80d5a8cdb7eb94e86babdcf24674b8b0563313a4b12459cbcc2ddf74
4
- data.tar.gz: 8053fbb8ebe24f105513e04e8856ea18614b86ab25b2b34e89ad8fdadabe2462
3
+ metadata.gz: 701b8f414dc39686af35c768fd62f176365f50e8e10cf5fc8d47fe8473fad702
4
+ data.tar.gz: ab412723cc02b1ce9e04c9fdcb51b0282f467d1f43dacbd415a4df4532111406
5
5
  SHA512:
6
- metadata.gz: d45c860e93f1c69bb3620f11dbb68179c3142bb63a8bb418e39b1c016c14d6bdba14971e3f5f3582a054dbb514f5b03c96dd8b22eeef77248058f99e8ed8ff5f
7
- data.tar.gz: 8c6f7c5cb040922d14a6b2828775bdfe6ad103cf7c1bdf07123b2722e81bd78f281caae194a803fe0852d2b3bee3ed3fdd182124918c103a23447828f1630ddc
6
+ metadata.gz: 1e5191bf7700df69a86c119fd2dadbc4e8ef26e5530b68dd60727057f6f9c08e6e6e1928040001986585dd9ebf4ea9f9032da6561687330dff4f887c654aea85
7
+ data.tar.gz: 20c01bf3d406b7431632aac6367ecb227fc735fef780cba4f4c760db1df898fbc34b5def97e0c46f96ed1742b9e5c575666b180845300892e8fe423566cb16ea
data/README.md CHANGED
@@ -24,7 +24,23 @@ The `from` domain must be one of your account's sendable sending domains, matche
24
24
 
25
25
  **A suppressed address raises.** Wakio suppresses addresses that have hard-bounced or complained, and accepts a send to one rather than refusing it. The response names each recipient's status, and `deliver!` raises `Wakio::Suppressed` (a `Wakio::DeliveryError`) when every recipient was suppressed, so a one-recipient mailer to a suppressed address fails loudly instead of reporting success. Retrying cannot help, so handle it rather than letting a job backend retry it: `rescue_from Wakio::Suppressed` in your mailer also catches it under `deliver_later`. A send where only some recipients were suppressed returns normally; check the returned response's `deliveries` for which ones.
26
26
 
27
- **A refusal a retry cannot fix raises `Wakio::Rejected`.** Every 4xx except `409 request_in_flight` raises it: a revoked key, an unverified `from` domain, an idempotency key reused with a different body. It is a `Wakio::DeliveryError` carrying `status`, `code` (the response's `error`, such as `"no_verified_sending_domain"`) and the parsed `body`, so a host app can `discard_on` it or alert instead of letting a job backend retry it for weeks. A 409 and every 5xx stay a plain `Wakio::DeliveryError`, because a retry is the right response to both.
27
+ **Refusals split by whether a retry can ever send the message.** Every error is a `Wakio::DeliveryError` carrying `status`, `code` (the response's `error`, such as `"invalid_request"`) and the parsed `body`.
28
+
29
+ - `Wakio::Rejected`: this message can never be sent. `400`, `413`, `422 invalid_request` and `422 idempotency_key_reused`. Report it and drop it.
30
+ - `Wakio::Misconfigured`: the account or its configuration is wrong, and the same message sends once that is fixed. `401 invalid_api_key`, `402 not_entitled`, `403 account_suspended`, `404`, `422 no_verified_sending_domain`, `422 unknown_template`, and any 4xx this gem does not know. Let it raise so the job retries and your error tracker shows it.
31
+ - A plain `Wakio::DeliveryError`: `409 request_in_flight` and every 5xx. Let it retry.
32
+
33
+ ```ruby
34
+ class ApplicationMailer < ActionMailer::Base
35
+ rescue_from Wakio::Suppressed do |error|
36
+ Rails.logger.info(error.message)
37
+ end
38
+
39
+ rescue_from Wakio::Rejected do |error|
40
+ Rails.error.report(error)
41
+ end
42
+ end
43
+ ```
28
44
 
29
45
  **Fifty recipients per request.** `to`, `cc` and `bcc` are counted together, once
30
46
  case-insensitive duplicates collapse, against Wakio's own contract limit — and the
@@ -3,13 +3,10 @@ require "net/http"
3
3
  require "uri"
4
4
 
5
5
  module Wakio
6
- DeliveryError = Class.new(StandardError)
7
- Suppressed = Class.new(DeliveryError)
8
-
9
- class Rejected < DeliveryError
6
+ class DeliveryError < StandardError
10
7
  attr_reader :status, :code, :body
11
8
 
12
- def initialize(message, status:, body:)
9
+ def initialize(message = nil, status: nil, body: nil)
13
10
  super(message)
14
11
  @status = status
15
12
  @body = body
@@ -17,6 +14,10 @@ module Wakio
17
14
  end
18
15
  end
19
16
 
17
+ Suppressed = Class.new(DeliveryError)
18
+ Rejected = Class.new(DeliveryError)
19
+ Misconfigured = Class.new(DeliveryError)
20
+
20
21
  # An ActionMailer delivery method, so a host app keeps every existing mailer
21
22
  # untouched and changes only configuration.
22
23
  class DeliveryMethod
@@ -31,6 +32,12 @@ module Wakio
31
32
  # obviously ours if it ever leaks into a message dump.
32
33
  IDEMPOTENCY_HEADER = "X-Wakio-Idempotency-Key".freeze
33
34
 
35
+ # Refusals about this message itself, which no retry can fix. Every other 4xx is about the
36
+ # account or its configuration and sends once an operator fixes it, so an unknown code fails
37
+ # towards retrying rather than towards dropping mail.
38
+ REJECTED_STATUSES = [ 400, 413 ].freeze
39
+ REJECTED_CODES = %w[invalid_request idempotency_key_reused].freeze
40
+
34
41
  def deliver!(mail)
35
42
  # Caller-supplied, never derived — and no key at all when the caller gives none.
36
43
  #
@@ -47,12 +54,7 @@ module Wakio
47
54
  # deliberate repeat", because the difference is intent rather than data.
48
55
  response = post(payload_for(mail), idempotency_key: mail[IDEMPOTENCY_HEADER]&.to_s)
49
56
  status = response.code.to_i
50
- unless status.between?(200, 299)
51
- message = "Wakio returned #{status}: #{response.body}"
52
- raise Rejected.new(message, status: status, body: parsed(response.body)) if status.between?(400, 499) && status != 409
53
-
54
- raise DeliveryError, message
55
- end
57
+ raise refusal(status, response.body) unless status.between?(200, 299)
56
58
 
57
59
  # A separate check from the status above, deliberately: `accepted_any?` only ever
58
60
  # returns true or raises today, but joining the two into one `&&` condition would
@@ -129,6 +131,17 @@ module Wakio
129
131
  unreadable!(response)
130
132
  end
131
133
 
134
+ def refusal(status, raw)
135
+ body = parsed(raw)
136
+ code = body["error"] if body.is_a?(Hash)
137
+ kind =
138
+ if !status.between?(400, 499) || status == 409 then DeliveryError
139
+ elsif REJECTED_STATUSES.include?(status) || REJECTED_CODES.include?(code) then Rejected
140
+ else Misconfigured
141
+ end
142
+ kind.new("Wakio returned #{status}: #{raw}", status: status, body: body)
143
+ end
144
+
132
145
  def parsed(body)
133
146
  JSON.parse(body.to_s)
134
147
  rescue JSON::ParserError
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wakio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - WorkersLab LLC