Skip to content

Commit

Permalink
Add default errors & update dev config
Browse files Browse the repository at this point in the history
  • Loading branch information
kmakiela committed Nov 6, 2019
1 parent abcdde7 commit 03154aa
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 12 deletions.
10 changes: 7 additions & 3 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ config :mongoose_push,
endpoint: "localhost",
appfile: "priv/fcm/token.json",
pool_size: 5,
mode: :prod
mode: :dev,
port: 4000,
tls_opts: []
]
]

Expand All @@ -42,7 +44,8 @@ config :mongoose_push,
endpoint: "localhost",
mode: :dev,
use_2197: true,
pool_size: 5
pool_size: 5,
tls_opts: []
],
prod: [
auth: %{
Expand All @@ -53,6 +56,7 @@ config :mongoose_push,
endpoint: "localhost",
mode: :prod,
use_2197: true,
pool_size: 5
pool_size: 5,
tls_opts: []
]
]
4 changes: 3 additions & 1 deletion lib/mongoose_push.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ defmodule MongoosePush do
@type service :: :fcm | :apns
@type mode :: :dev | :prod

@type error :: :no_matching_pool
@type error ::
:no_matching_pool
| {:generic, atom}

@doc """
Push notification defined by `request` to device with `device_id`.
Expand Down
5 changes: 3 additions & 2 deletions lib/mongoose_push/service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule MongoosePush.Service do
| :unspecified
| :service_internal
| :payload_too_large
| :unknown

@type error_reason :: atom

Expand All @@ -25,10 +26,10 @@ defmodule MongoosePush.Service do
@type error :: {error_type, error_reason}

@callback push(notification(), String.t(), Application.pool_name(), options()) ::
:ok | {:error, term}
:ok | {:error, error} | {:error, MongoosePush.error()}
@callback prepare_notification(String.t(), MongoosePush.request(), Application.pool_name()) ::
notification()
@callback supervisor_entry([Application.pool_definition()] | nil) :: {module(), term()}
@callback choose_pool(MongoosePush.mode()) :: Application.pool_name() | nil
@callback unify_error(error_reason) :: error
@callback unify_error(error_reason) :: error | MongoosePush.error()
end
14 changes: 11 additions & 3 deletions lib/mongoose_push/service/apns.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule MongoosePush.Service.APNS do
end

@spec push(Service.notification(), String.t(), Application.pool_name(), Service.options()) ::
:ok | {:error, Service.error()}
:ok | {:error, Service.error()} | {:error, MongoosePush.error()}
def push(notification, _device_id, pool, _opts \\ []) do
case APNS.push(pool, notification, is_sync: true) do
:ok ->
Expand All @@ -65,9 +65,17 @@ defmodule MongoosePush.Service.APNS do
Sparrow.PoolsWarden.choose_pool({:apns, mode}, tags)
end

@spec unify_error(Service.error_reason()) :: Service.error()
@spec unify_error(Service.error_reason()) :: Service.error() | MongoosePush.error()
def unify_error(reason) do
ErrorHandler.translate_error_reason(reason)
{type, reason_} = ErrorHandler.translate_error_reason(reason)

case type do
:unknown ->
{:generic, reason_}

_ ->
{type, reason_}
end
end

defp maybe(notification, :add_mutable_content, true),
Expand Down
2 changes: 2 additions & 0 deletions lib/mongoose_push/service/apns/error_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ defmodule MongoosePush.Service.APNS.ErrorHandler do
def translate_error_reason(:InternalServerError), do: {:service_internal, :InternalServerError}

def translate_error_reason(:PayloadTooLarge), do: {:payload_too_large, :PayloadTooLarge}

def translate_error_reason(reason), do: {:unknown, reason}
end
14 changes: 11 additions & 3 deletions lib/mongoose_push/service/fcm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule MongoosePush.Service.FCM do
end

@spec push(Service.notification(), String.t(), Application.pool_name(), Service.options()) ::
:ok | {:error, Service.error()}
:ok | {:error, Service.error()} | {:error, MongoosePush.error()}
def push(notification, _device_id, pool, _opts \\ []) do
case FCM.push(pool, notification, is_sync: true) do
:ok ->
Expand All @@ -73,8 +73,16 @@ defmodule MongoosePush.Service.FCM do
defp maybe(notification, _function, nil), do: notification
defp maybe(notification, function, arg), do: apply(Android, function, [notification, arg])

@spec unify_error(Service.error_reason()) :: Service.error()
@spec unify_error(Service.error_reason()) :: Service.error() | MongoosePush.error()
def unify_error(reason) do
ErrorHandler.translate_error_reason(reason)
{type, reason_} = ErrorHandler.translate_error_reason(reason)

case type do
:unknown ->
{:generic, reason_}

_ ->
{type, reason_}
end
end
end
1 change: 1 addition & 0 deletions lib/mongoose_push/service/fcm/error_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ defmodule MongoosePush.Service.FCM.ErrorHandler do
def translate_error_reason(:THIRD_PARTY_AUTH_ERROR), do: {:auth, :THIRD_PARTY_AUTH_ERROR}
def translate_error_reason(:UNAVAILABLE), do: {:service_internal, :UNAVAILABLE}
def translate_error_reason(:INTERNAL), do: {:service_internal, :INTERNAL}
def translate_error_reason(reason), do: {:unknown, reason}
end

0 comments on commit 03154aa

Please sign in to comment.