Merge branch 'master' into springerigor-read-timeout-request-option

This commit is contained in:
Mattia 2019-10-17 11:02:23 +01:00 committed by GitHub
commit 9de70e0184
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 147 additions and 204 deletions

View File

@ -1,164 +0,0 @@
version: 2.1
# Share Steps: glued in using the YAML alias
# See https://circleci.com/blog/circleci-hacks-reuse-yaml-in-your-circleci-config-with-yaml/
shared_ruby_steps: &shared_ruby_steps
parameters:
run-cc-reporter:
type: boolean
default: false
steps:
- attach_workspace:
at: .
- restore_cache:
keys:
- "{{ .Environment.CACHE_KEY_PREFIX }}-v1-bundler-deps-{{ .Branch }}"
- run:
name: Bundle Install
command: bundle install --path vendor/bundle --jobs 7 --retry 15
- when:
condition: << parameters.run-cc-reporter >>
steps:
- run:
name: Setup Code Climate test-reporter
command: |
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
./cc-test-reporter before-build
- run: mkdir -p ~/test-results/rspec
- run:
name: Run tests
command: bundle exec rspec --require rspec_junit_formatter --format progress --format RspecJunitFormatter --out ~/test-results/rspec/results.xml
- when:
condition: << parameters.run-cc-reporter >>
steps:
- run:
name: Run Code Climate test-reporter
command: ./cc-test-reporter after-build --coverage-input-type simplecov --exit-code $?
- save_cache:
key: "{{ .Environment.CACHE_KEY_PREFIX }}-v1-bundler-deps-{{ .Branch }}"
paths:
- ./vendor/bundle
- store_test_results:
path: ~/test-results/rspec
- store_artifacts:
path: ./Gemfile.lock
- persist_to_workspace:
root: .
paths:
- ./vendor/bundle
jobs:
checkout_code:
docker:
- image: circleci/ruby:2.6
steps:
- checkout
- persist_to_workspace:
root: .
paths:
- .
linting:
docker:
- image: circleci/ruby:2.6
steps:
- attach_workspace:
at: .
- run: mkdir -p ~/test-results/linting
- run:
name: RuboCop
command: |
gem install rubocop rubocop-junit_formatter rubocop-performance --no-document
rubocop --require rubocop/formatter/junit_formatter \
--require rubocop-performance \
--format progress \
--format RuboCop::Formatter::JUnitFormatter \
--out ~/test-results/linting/rubocop.xml
when: always
- run:
name: Yard-Junk
command: |
gem install yard-junk --no-document
yard-junk --path lib
when: always
- store_test_results:
path: ~/test-results/linting
- store_artifacts:
path: ~/test-results/linting
ruby26:
docker:
- image: circleci/ruby:2.6
<<: *shared_ruby_steps
ruby25:
docker:
- image: circleci/ruby:2.5
<<: *shared_ruby_steps
ruby24:
docker:
- image: circleci/ruby:2.4
<<: *shared_ruby_steps
ruby23:
docker:
- image: circleci/ruby:2.3
<<: *shared_ruby_steps
# Currently not in use
jruby92:
docker:
- image: circleci/jruby:9.2
environment:
JRUBY_OPTS: "--debug"
<<: *shared_ruby_steps
deploy:
docker:
- image: circleci/ruby:2.6
steps:
- checkout
- run:
name: Setup Rubygems
command: bash .circleci/setup-rubygems.sh
- run:
name: Publish to Rubygems
command: |
gem build faraday.gemspec
gem push "faraday-$(git describe --tags).gem"
workflows:
version: 2
test:
jobs:
- checkout_code
- linting:
requires:
- checkout_code
- ruby26:
requires:
- linting
run-cc-reporter: true
- ruby25:
requires:
- linting
- ruby24:
requires:
- linting
- ruby23:
requires:
- linting
- deploy:
requires:
- ruby23
- ruby24
- ruby25
- ruby26
filters:
tags:
only: /.*/
branches:
ignore: /.*/

View File

@ -1,15 +0,0 @@
#
# "Publishing RubyGems using Circle CI 2.0" explains how this works:
#
# https://medium.com/@pezholio/publishing-rubygems-using-circle-ci-2-0-1dbf06ae9942
#
# - Get an API key from your profile page at RubyGems.org
# - Add the API key as an Environment variable in your repos CircleCI
# Project Settings/Build Settings/Environment Variables
# - Have this script execute in the deploy stage of the CI build
# - Now you can "gem push"
mkdir ~/.gem
echo -e "---\r\n:rubygems_api_key: $RUBYGEMS_API_KEY" > ~/.gem/credentials
chmod 0600 /home/circleci/.gem/credentials

View File

@ -13,7 +13,7 @@ note that so we can add it to the [Changelog][].
### Required Checks
Before pushing your code and opening a PR, we recommend you run the following checks to avoid
our CircleCI Workflow to block your contribution.
our GitHub Actions Workflow to block your contribution.
```bash
# Run unit tests and check code coverage

68
.github/workflows/ci.yml vendored Normal file
View File

@ -0,0 +1,68 @@
name: CI
on: [push, pull_request]
env:
GIT_COMMIT_SHA: ${{ github.sha }}
GIT_BRANCH: ${{ github.ref }}
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
jobs:
linting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Set up Ruby 2.6
uses: actions/setup-ruby@v1
with:
ruby-version: 2.6.x
- name: Rubocop
run: |
gem install rubocop rubocop-performance --no-document
rubocop --require rubocop-performance --format progress
- name: Yard-Junk
run: |
gem install yard-junk --no-document
yard-junk --path lib
build:
needs: [linting]
runs-on: ubuntu-latest
strategy:
matrix:
ruby: [2.3.x, 2.4.x, 2.5.x, 2.6.x]
steps:
- uses: actions/checkout@v1
- name: Install dependencies
run: |
sudo apt-get install libcurl4-openssl-dev
- name: Set up Ruby
uses: actions/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
- name: Build
run: |
gem install bundler
bundle install --jobs 4 --retry 3
- name: Setup Code Climate
if: matrix.ruby == '2.6.x'
run: |
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
./cc-test-reporter before-build
- name: Test
run: bundle exec rake
- name: Run Code Climate Test Reporter
if: success() && matrix.ruby == '2.6.x'
run: ./cc-test-reporter after-build --coverage-input-type simplecov --exit-code $?

29
.github/workflows/publish.yml vendored Normal file
View File

@ -0,0 +1,29 @@
name: Publish
on:
release:
types: [published]
jobs:
build:
name: Publish to Rubygems
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Set up Ruby 2.6
uses: actions/setup-ruby@v1
with:
version: 2.6.x
- name: Publish to RubyGems
run: |
mkdir -p $HOME/.gem
touch $HOME/.gem/credentials
chmod 0600 $HOME/.gem/credentials
printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
gem build faraday.gemspec
gem push faraday-*.gem
env:
GEM_HOST_API_KEY: ${{ secrets.RUBYGEMS_AUTH_TOKEN }}

View File

@ -1,7 +1,7 @@
# ![Faraday](./docs/assets/img/repo-card-slim.png)
[![Gem Version](https://badge.fury.io/rb/faraday.svg)](https://rubygems.org/gems/faraday)
[![CircleCI](https://circleci.com/gh/lostisland/faraday/tree/master.svg?style=svg)](https://circleci.com/gh/lostisland/faraday/tree/master)
![GitHub Actions CI](https://github.com/lostisland/faraday/workflows/CI/badge.svg)
[![Test Coverage](https://api.codeclimate.com/v1/badges/f869daab091ceef1da73/test_coverage)](https://codeclimate.com/github/lostisland/faraday/test_coverage)
[![Maintainability](https://api.codeclimate.com/v1/badges/f869daab091ceef1da73/maintainability)](https://codeclimate.com/github/lostisland/faraday/maintainability)
[![Gitter](https://badges.gitter.im/lostisland/faraday.svg)](https://gitter.im/lostisland/faraday?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
@ -18,7 +18,7 @@ Need more details? See the [Faraday API Documentation][apidoc] to see how it wor
## Supported Ruby versions
This library aims to support and is [tested against][circle_ci] the following Ruby
This library aims to support and is [tested against][actions] the following Ruby
implementations:
* Ruby 2.3+
@ -49,7 +49,7 @@ But before you start coding, please read our [Contributing Guide][contributing]
[faraday_team]: https://lostisland.github.io/faraday/team
[contributing]: https://github.com/lostisland/faraday/blob/master/.github/CONTRIBUTING.md
[apidoc]: http://www.rubydoc.info/gems/faraday
[circle_ci]: https://circleci.com/gh/lostisland/faraday
[actions]: https://github.com/lostisland/faraday/actions
[jruby]: http://jruby.org/
[rubinius]: http://rubini.us/
[license]: LICENSE.md

View File

@ -9,26 +9,9 @@ top_name: Back to Middleware
top_link: ./list
---
The `RaiseError` middleware checks the response HTTP code and raises an exception if it is a 4xx or 5xx code.
Specific exceptions are raised based on the HTTP Status code, according to the list below:
```
## 4xx HTTP codes
400 => Faraday::BadRequestError
401 => Faraday::UnauthorizedError
403 => Faraday::ForbiddenError
404 => Faraday::ResourceNotFound
407 => Faraday::ProxyAuthError
409 => Faraday::ConflictError
422 => Faraday::UnprocessableEntityError
4xx => Faraday::ClientError (all exceptions above inherit from this one.
## 5xx HTTP codes
5xx => Faraday::ServerError
```
All exceptions classes listed above inherit from `Faraday::Error`, and are initialized providing
the response `status`, `headers` and `body`, available for you to access on rescue:
The `RaiseError` middleware raises a `Faraday::Error` exception if an HTTP
response returns with a 4xx or 5xx status code. All exceptions are initialized
providing the response `status`, `headers`, and `body`.
```ruby
begin
@ -37,5 +20,30 @@ rescue Faraday::ResourceNotFound => e
e.response[:status] #=> 404
e.response[:headers] #=> { ... }
e.response[:body] #=> "..."
end
end
```
Specific exceptions are raised based on the HTTP Status code, according to the list below:
An HTTP status in the 400-499 range typically represents an error
by the client. They raise error classes inheriting from `Faraday::ClientError`.
* 400 => `Faraday::BadRequestError`
* 401 => `Faraday::UnauthorizedError`
* 403 => `Faraday::ForbiddenError`
* 404 => `Faraday::ResourceNotFound`
* 407 => `Faraday::ProxyAuthError`
* 409 => `Faraday::ConflictError`
* 422 => `Faraday::UnprocessableEntityError`
* 4xx => `Faraday::ClientError`
An HTTP status in the 500-599 range represents a server error, and raises a
`Faraday::ServerError` exception.
* 5xx => `Faraday::ServerError`
The HTTP response status may be nil due to a malformed HTTP response from the
server, or a bug in the underlying HTTP library. It inherits from
`Faraday::ServerError`.
* nil => `Faraday::NilStatusError`

View File

@ -80,6 +80,14 @@ module Faraday
end
end
# Raised by Faraday::Response::RaiseError in case of a nil status in response.
class NilStatusError < ServerError
def initialize(_exc, response: nil)
message = 'http status could not be derived from the server response'
super(message, response)
end
end
# A unified error for failed connections.
class ConnectionFailed < Error
end

View File

@ -32,6 +32,8 @@ module Faraday
raise Faraday::ClientError, response_values(env)
when ServerErrorStatuses
raise Faraday::ServerError, response_values(env)
when nil
raise Faraday::NilStatusError, response: response_values(env)
end
end

View File

@ -14,6 +14,7 @@ RSpec.describe Faraday::Response::RaiseError do
stub.get('conflict') { [409, { 'X-Reason' => 'because' }, 'keep looking'] }
stub.get('unprocessable-entity') { [422, { 'X-Reason' => 'because' }, 'keep looking'] }
stub.get('4xx') { [499, { 'X-Reason' => 'because' }, 'keep looking'] }
stub.get('nil-status') { [nil, { 'X-Reason' => 'bailout' }, 'fail'] }
stub.get('server-error') { [500, { 'X-Error' => 'bailout' }, 'fail'] }
end
end
@ -72,6 +73,12 @@ RSpec.describe Faraday::Response::RaiseError do
end
end
it 'raises Faraday::NilStatusError for nil status in response' do
expect { conn.get('nil-status') }.to raise_error(Faraday::NilStatusError) do |ex|
expect(ex.message).to eq('http status could not be derived from the server response')
end
end
it 'raises Faraday::ClientError for other 4xx responses' do
expect { conn.get('4xx') }.to raise_error(Faraday::ClientError) do |ex|
expect(ex.message).to eq('the server responded with status 499')