Nicer error API
Both the combination of positional argument and naming does not feel like Ruby. This change makes `is_error` argument a keyword argument called `error`. Thanks to this, the usage is more self-explanatory: ```ruby response = Response.new([], error: true) ```
This commit is contained in:
parent
33258623a7
commit
6689db2ca0
1 changed files with 94 additions and 6 deletions
|
|
@ -3,15 +3,26 @@
|
|||
module MCP
|
||||
class Tool
|
||||
class Response
|
||||
attr_reader :content, :is_error
|
||||
NOT_GIVEN = Object.new.freeze
|
||||
|
||||
attr_reader :content
|
||||
|
||||
def initialize(content, deprecated_error = NOT_GIVEN, error: false)
|
||||
if deprecated_error != NOT_GIVEN
|
||||
warn("Passing `error` with the 2nd argument of `Response.new` is deprecated. Use keyword argument like `Response.new(content, error: error)` instead.", uplevel: 1)
|
||||
error = deprecated_error
|
||||
end
|
||||
|
||||
def initialize(content, is_error = false)
|
||||
@content = content
|
||||
@is_error = is_error
|
||||
@error = error
|
||||
end
|
||||
|
||||
def error?
|
||||
!!@error
|
||||
end
|
||||
|
||||
def to_h
|
||||
{ content:, isError: is_error }.compact
|
||||
{ content:, isError: error? }.compact
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue