mirror of
https://github.com/ngrok/ngrok-api-ruby.git
synced 2026-05-17 16:50:40 +00:00
Add generic error for all >= 400
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user