File: exception.rb

package info (click to toggle)
ruby-aliyun-sdk 0.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 792 kB
  • sloc: ruby: 7,909; ansic: 204; makefile: 4
file content (114 lines) | stat: -rw-r--r-- 3,440 bytes parent folder | download
1
2
3
4
5
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
42
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
78
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
108
109
110
111
112
113
114
# -*- encoding: utf-8 -*-

require 'nokogiri'

module Aliyun
  module OSS

    ##
    # ServerError represents exceptions from the OSS
    # service. i.e. Client receives a HTTP response whose status is
    # NOT OK. #message provides the error message and #to_s gives
    # detailed information probably including the OSS request id.
    #
    class ServerError < Common::Exception

      attr_reader :http_code, :error_code, :message, :request_id

      def initialize(response)
        @http_code = response.code
        @attrs = {'RequestId' => get_request_id(response)}

        doc = Nokogiri::XML(response.body) do |config|
          config.options |= Nokogiri::XML::ParseOptions::NOBLANKS
        end rescue nil

        if doc and doc.root
          doc.root.children.each do |n|
            @attrs[n.name] = n.text
          end
        end

        @error_code = @attrs['Code']
        @message = @attrs['Message']
        @request_id = @attrs['RequestId']
      end

      def message
        msg = @attrs['Message'] || "UnknownError[#{http_code}]."
        "#{msg} RequestId: #{request_id}"
      end

      def to_s
        @attrs.merge({'HTTPCode' => @http_code}).map do |k, v|
          [k, v].join(": ")
        end.join(", ")
      end

      private

      def get_request_id(response)
        r = response.headers[:x_oss_request_id] if response.headers
        r.to_s
      end

    end # ServerError

    class CallbackError < ServerError
    end # CallbackError

    ##
    # ClientError represents client exceptions caused mostly by
    # invalid parameters.
    #
    class ClientError < Common::Exception
    end # ClientError

    ##
    # CrcInconsistentError will be raised after a upload operation,
    # when the local crc is inconsistent with the response crc from server. 
    #
    class CrcInconsistentError < Common::Exception; end

    ##
    # FileInconsistentError happens in a resumable upload transaction,
    # when the file to upload has changed during the uploading
    # process. Which means the transaction cannot go on. Or user may
    # have inconsistent data uploaded to OSS.
    #
    class FileInconsistentError < ClientError; end

    ##
    # ObjectInconsistentError happens in a resumable download transaction,
    # when the object to download has changed during the downloading
    # process. Which means the transaction cannot go on. Or user may
    # have inconsistent data downloaded to OSS.
    #
    class ObjectInconsistentError < ClientError; end

    ##
    # PartMissingError happens in a resumable download transaction,
    # when a downloaded part cannot be found as the client tries to
    # resume download. The process cannot go on until the part is
    # restored.
    #
    class PartMissingError < ClientError; end

    ##
    # PartMissingError happens in a resumable download transaction,
    # when a downloaded part has changed(MD5 mismatch) as the client
    # tries to resume download. The process cannot go on until the
    # part is restored.
    #
    class PartInconsistentError < ClientError; end

    ##
    # CheckpointBrokenError happens in a resumable upload/download
    # transaction, when the client finds the checkpoint file has
    # changed(MD5 mismatch) as it tries to resume upload/download. The
    # process cannot go on until the checkpoint file is restored.
    #
    class CheckpointBrokenError < ClientError; end

  end # OSS
end # Aliyun