Coder Social home page Coder Social logo

Rename ResultExt::to_field_err? about juniper HOT 4 CLOSED

theduke avatar theduke commented on August 15, 2024
Rename ResultExt::to_field_err?

from juniper.

Comments (4)

mhallin avatar mhallin commented on August 15, 2024

Sounds reasonable, but while we're on the topic of field errors... I feel that error handling is a bit unergonomic right now. For example, if you're doing stuff that can fail, you've got two options in practice:

  • Import juniper::ResultExt, write e.g. let txn = db.transaction().to_field_result()?;
  • Use jtry!, e.g. let txn = jtry(db.transaction());

Converting error types yourself feels like a shortcoming on Juniper's side; why should a user have to care about that? On the other hand, using the macro looks a bit weird when you're chaining operations that can fail: jtry!(jtry!(File.open(...)).read(...)).

So, I wonder if we could make the macros support any kind of Result<T, E> (or at least E: Display) and call .to_field_result() on the result automatically? Then we could maybe even remove the ResultExt trait and jtry! macro.

from juniper.

srijs avatar srijs commented on August 15, 2024

IIRC both the try macro as well as the ? operator provide type upcasting: https://github.com/rust-lang/rfcs/blob/master/text/0243-trait-based-exception-handling.md#exception-type-upcasting.

So an alternative could be to define a concrete type FieldError implementing From:

struct FieldError {
  message: String
}

type FieldResult<T> = Result<T, FieldError>;

impl<E> From<E> for FieldError where E: Display {
  fn from(err: E) -> FieldError {
    FieldError { message: format!("{}", err) }
  }
}

This would enable people to use the ? operator without to_field_result in field bodies:

field foo()  {
  let txn = db.transaction()?;
  ...
}

... as well as enable the use of try! instead of jtry!, meaning that both jtry and ResultExt could be deprecated.

from juniper.

theduke avatar theduke commented on August 15, 2024

Deprecating the macros is something we already talked about.

I'll write more here later, but just for now: #40 is also relevant for this.

from juniper.

theduke avatar theduke commented on August 15, 2024

Both ResultExt and the jtry macro were removed in 0.9.

from juniper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.