Class: X::AltTextFailed

Inherits:
Uploader::Error show all
Defined in:
x-uploader/lib/x/uploader/alt_text_failed.rb

Overview

Error raised when media uploaded with alt text is uploaded, but its alt text cannot be added

The alt text is added once the media is uploaded, and processed, so the media the upload billed is not lost to a failure to add it, whether the API refuses it or the on_response hook of the client raises: the error holds the media, which can be attached to a post, or given its alt text again with add_alt_text. The error that failed to add it is the cause, whose message the message ends with.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, media: nil) ⇒ AltTextFailed

Initialize the error with the media that was uploaded

The message is the one given, or else names the media by its identifier, when media that holds one was given.

Examples:

Raise the error for media whose alt text could not be added

raise X::AltTextFailed.new(media: media)

Raise the error with a message of its own, as a test stub may

raise X::AltTextFailed, "Alt text could not be added"

Parameters:

  • message (String, nil) (defaults to: nil) —

    the message, or nil for one that names the media

  • media (UploadedMedia, Hash{String => Object}, nil) (defaults to: nil) —

    the media that was uploaded, a Hash of which is held as uploaded media



61
62
63
64
# File 'x-uploader/lib/x/uploader/alt_text_failed.rb', line 61

def initialize(message = nil, media: nil)
  @media = media.is_a?(Hash) ? UploadedMedia.new(media) : media
  super(message || ["Media", media&.[]("id"), "was uploaded, but its alt text could not be added"].compact.join(" "))
end

Instance Attribute Details

#media ⇒ UploadedMedia? (readonly)

The media that was uploaded, without its alt text

Examples:

Add the alt text again later

rescue X::AltTextFailed => e
  client.add_alt_text(e.media, "A cat asleep on a keyboard")

Returns:

  • (UploadedMedia, nil) —

    the uploaded media, or nil if none was given



46
47
48
# File 'x-uploader/lib/x/uploader/alt_text_failed.rb', line 46

def media
  @media
end

Instance Method Details

#to_s ⇒ String

The message, ending with why the alt text could not be added

It ends with the message of the error that failed to add the alt text, which is the cause, if there is one, whether the message was given or named the media.

Examples:

Read why the alt text could not be added

error.message # => "Media 7 was uploaded, but its alt text could not be added: Bad Request"

Returns:

  • (String) —

    the message



75
# File 'x-uploader/lib/x/uploader/alt_text_failed.rb', line 75

def to_s = [super, cause&.message].compact.join(": ")