Add generic error for all >= 400

This commit is contained in:
David Parker
2021-09-22 10:16:32 -06:00
parent fa741fcd63
commit 05c9cfd9bd
6 changed files with 35 additions and 13 deletions
+1 -8
View File
@@ -1,5 +1,5 @@
name: CI
on: [push, pull_request]
on: [push]
jobs:
build:
runs-on: ubuntu-latest
@@ -9,13 +9,6 @@ jobs:
steps:
- name: Check out repository code
uses: actions/checkout@v2
- name: Do some action caching
uses: actions/cache@v1
with:
path: vendor/bundle
key: ${{ runner.os }}-gem-${{ hashFiles('**/Gemfile.lock') }}
restore-keys: |
${{ runner.os }}-gem-
- name: Set up Ruby
uses: actions/setup-ruby@v1
with:
+14
View File
@@ -0,0 +1,14 @@
# frozen_string_literal: true
module NgrokAPI
##
# Base Error class for all Ngrok Errors
class Error < StandardError
attr_reader :response
def initialize(msg: "An error occurred with the NgrokAPI", response: nil)
@response = response
super(msg)
end
end
end
+2 -2
View File
@@ -4,12 +4,12 @@ module NgrokAPI
module Errors
##
# Error representing a 404 not found
class NotFoundError < StandardError
class NotFoundError < NgrokAPI::Error
attr_reader :response
def initialize(msg: "Resource not found", response: nil)
@response = response
super(msg)
super(msg: msg, response: response)
end
end
end
+6 -2
View File
@@ -103,8 +103,12 @@ module NgrokAPI
resp = Net::HTTP.start(uri.hostname, uri.port, req_options(uri)) do |http|
http.request(req, data)
end
if danger && resp.code == "404"
raise NgrokAPI::Errors::NotFoundError.new(response: resp)
if danger
if resp.code.to_i == 404
raise NgrokAPI::Errors::NotFoundError.new(response: resp)
elsif resp.code.to_i >= 400
raise NgrokAPI::Error.new(response: resp)
end
end
if resp.body && resp.body != ''
JSON.parse(resp.body)
+4 -1
View File
@@ -1,4 +1,7 @@
module NgrokAPI
# rubocop:disable Layout/SpaceAroundOperators
# The current version of the gem
VERSION = '0.0.1'.freeze
VERSION="0.0.1".freeze
# rubocop:enable Layout/SpaceAroundOperators
end
+8
View File
@@ -31,6 +31,14 @@ RSpec.describe NgrokAPI::HttpClient do
@client.get(url, danger: true)
end.to raise_error(NgrokAPI::Errors::NotFoundError)
end
it "will raise a NgrokAPI::Error if >= 400" do
url = "#{base_url}#{path}/#{api_key_result["id"]}"
stub_request(:get, url).to_return(body: nil, status: 400)
expect do
@client.get(url, danger: true)
end.to raise_error(NgrokAPI::Error)
end
end
describe "#list" do