Class: SlackMessage::ErrorHandling

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_message/error_handling.rb

Constant Summary collapse

PERMISSIONS_ERRORS =
["token_revoked", "token_expired", "invalid_auth", "not_authed",
"team_access_not_granted", "no_permission", "missing_scope",
"not_allowed_token_type", "ekm_access_denied"]

Class Method Summary collapse

Class Method Details

.blocks_message(blocks) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/slack_message/error_handling.rb', line 135

def self.blocks_message(blocks)
  if SlackMessage::Configuration.debugging?
    JSON.pretty_generate(blocks)
  else
    "[Enable debugging in configuration to view block data.]"
  end
end

.raise_delete_response_errors(response, message, profile) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/slack_message/error_handling.rb', line 79

def self.raise_delete_response_errors(response, message, profile)
  body  = JSON.parse(response.body)
  error = body.fetch("error", "")

  if error == "channel_not_found"
    raise SlackMessage::ApiError, "Tried to delete Slack message in non-existent channel '#{message.channel}'"

  elsif error == "invalid_scheduled_message_id"
    raise SlackMessage::ApiError, "Can't delete message because the ID was invalid, or the message has already posted (#{message.scheduled_message_id})"
  elsif error == "message_not_found"
    raise SlackMessage::ApiError, "Tried to delete Slack message, but the message wasn't found (timestamp '#{message.timestamp}' for channel '#{message.channel}')"
  elsif error == "cant_delete_message"
    raise SlackMessage::ApiError, "Can't delete message because '#{profile[:handle]}' doesn't have permission to"
  elsif error == "compliance_exports_prevent_deletion"
    raise SlackMessage::ApiError, "Can't delete message because team compliance settings prevent it"


  elsif PERMISSIONS_ERRORS.include?(error)
    raise SlackMessage::ApiError, "Couldn't delete Slack message because the API key for profile '#{profile[:handle]}' is wrong, or the app has insufficient permissions (#{error})"
  elsif ["rate_limited", "ratelimited"].include?(error)
    raise SlackMessage::ApiError, "Couldn't delete Slack message because you've reached your rate limit"
  elsif response.code == "302"
    raise SlackMessage::ApiError, "Got 302 response while deleting a message. Check your API key for profile '#{profile[:handle]}'"
  elsif response.code != "200"
    raise SlackMessage::ApiError, "Got an error back from the Slack API (HTTP #{response.code}):\n#{response.body}"
  elsif !(error.nil? || error == "")
    raise SlackMessage::ApiError, "Received error response '#{error}' from Slack during message delete:\n#{response.body}"
  end
end

.raise_post_response_errors(response, params, profile) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/slack_message/error_handling.rb', line 6

def self.raise_post_response_errors(response, params, profile)
  body  = JSON.parse(response.body)
  error = body.fetch("error", "")

  if error == "invalid_blocks"
    raise SlackMessage::ApiError, "Couldn't send Slack message because the request contained invalid blocks:\n#{blocks_message(params[:blocks])}"
  elsif error == "invalid_blocks_format"
    raise SlackMessage::ApiError, "Couldn't send Slack message because blocks is not a valid JSON object or doesn't match the Block Kit syntax:\n#{blocks_message(params[:blocks])}"
  elsif error == "channel_not_found"
    raise SlackMessage::ApiError, "Tried to send Slack message to non-existent channel or user '#{params[:channel]}'"

  # scheduling messages
  elsif error == "invalid_time"
    raise SlackMessage::ApiError, "Couldn't schedule Slack message because you requested an invalid time '#{params[:post_at]}'"
  elsif error == "time_in_past"
    raise SlackMessage::ApiError, "Couldn't schedule Slack message because you requested a time in the past (or too close to now) '#{params[:post_at]}'"
  elsif error == "time_too_far"
    raise SlackMessage::ApiError, "Couldn't schedule Slack message because you requested a time more than 120 days in the future '#{params[:post_at]}'"


  elsif PERMISSIONS_ERRORS.include?(error)
    raise SlackMessage::ApiError, "Couldn't send Slack message because the API key for profile '#{profile[:handle]}' is wrong, or the app has insufficient permissions (#{error})"
  elsif error == "message_too_long"
    raise SlackMessage::ApiError, "Tried to send Slack message, but the message was too long"
  elsif error == "invalid_arguments"
    raise SlackMessage::ApiError, "Tried to send Slack message with invalid payload"
  elsif ["rate_limited", "ratelimited"].include?(error)
    raise SlackMessage::ApiError, "Couldn't send Slack message because you've reached your rate limit"
  elsif response.code == "302"
    raise SlackMessage::ApiError, "Got 302 response while posting to Slack. Check your API key for profile '#{profile[:handle]}'"
  elsif response.code != "200"
    raise SlackMessage::ApiError, "Got an error back from the Slack API (HTTP #{response.code}):\n#{response.body}"
  elsif !(error.nil? || error == "")
    raise SlackMessage::ApiError, "Received error response '#{error}' from Slack during message posting:\n#{response.body}"
  end
end

.raise_update_response_errors(response, params, profile) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/slack_message/error_handling.rb', line 43

def self.raise_update_response_errors(response, params, profile)
  body  = JSON.parse(response.body)
  error = body.fetch("error", "")

  if error == "invalid_blocks"
    raise SlackMessage::ApiError, "Couldn't update Slack message because the request contained invalid blocks:\n#{blocks_message(params[:blocks])}"
  elsif error == "invalid_blocks_format"
    raise SlackMessage::ApiError, "Couldn't update Slack message because blocks is not a valid JSON object or doesn't match the Block Kit syntax:\n#{blocks_message(params[:blocks])}"
  elsif error == "channel_not_found"
    raise SlackMessage::ApiError, "Tried to update Slack message to non-existent channel or user '#{params[:channel]}'"

  elsif error == "message_not_found"
    raise SlackMessage::ApiError, "Tried to update Slack message, but the message wasn't found (timestamp '#{params[:ts]}' for channel '#{params[:channel]}'"
  elsif error == "cant_update_message"
    raise SlackMessage::ApiError, "Couldn't update message because the message type isn't able to be updated, or #{profile[:handle]} isn't allowed to update it"
  elsif error == "edit_window_closed"
    raise SlackMessage::ApiError, "Couldn't update message because it's too old"


  elsif PERMISSIONS_ERRORS.include?(error)
    raise SlackMessage::ApiError, "Couldn't update Slack message because the API key for profile '#{profile[:handle]}' is wrong, or the app has insufficient permissions (#{error})"
  elsif error == "message_too_long"
    raise SlackMessage::ApiError, "Tried to update Slack message, but the message was too long"
  elsif error == "invalid_arguments"
    raise SlackMessage::ApiError, "Tried to update Slack message with invalid payload"
  elsif ["rate_limited", "ratelimited"].include?(error)
    raise SlackMessage::ApiError, "Couldn't update Slack message because you've reached your rate limit"
  elsif response.code == "302"
    raise SlackMessage::ApiError, "Got 302 response while updating a message. Check your API key for profile '#{profile[:handle]}'"
  elsif response.code != "200"
    raise SlackMessage::ApiError, "Got an error back from the Slack API (HTTP #{response.code}):\n#{response.body}"
  elsif !(error.nil? || error == "")
    raise SlackMessage::ApiError, "Received error response '#{error}' from Slack during message update:\n#{response.body}"
  end
end

.raise_user_lookup_response_errors(response, email, profile) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/slack_message/error_handling.rb', line 109

def self.raise_user_lookup_response_errors(response, email, profile)
  begin
    payload = JSON.parse(response.body)
  rescue
    raise SlackMessage::ApiError, "Unable to parse JSON response from Slack API\n#{response.body}"
  end

  error = payload["error"]

  if error == "users_not_found"
    raise SlackMessage::ApiError, "Couldn't find a user with the email '#{email}'"


  elsif PERMISSIONS_ERRORS.include?(error)
    raise SlackMessage::ApiError, "Couldn't look up users because the API key for profile '#{profile[:handle]}' is wrong, or the app has insufficient permissions (#{error})"
  elsif error
    raise SlackMessage::ApiError, "Received error response from Slack during user lookup:\n#{response.body}"
  elsif response.code == "302"
    raise SlackMessage::ApiError, "Got 302 response during user lookup. Check your API key for profile '#{profile[:handle]}'"
  elsif response.code != "200"
    raise SlackMessage::ApiError, "Got an error back from the Slack API (HTTP #{response.code}):\n#{response.body}"
  elsif !(error.nil? || error == "")
    raise SlackMessage::ApiError, "Received error response from Slack during user lookup:\n#{response.body}"
  end
end