Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7560787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:59:33+00:00 2026-05-30T12:59:33+00:00

I am using scalatra to export a MongoDB data to JSon, my actions are

  • 0

I am using scalatra to “export” a MongoDB data to JSon, my actions are very simple, like:

   get("/") {
      val title = db.get_collection("main", "api", "title")
      send_json(title)
    }

I want to send a HTTP error and a text if something go wrong, on the other side it will be converted to something meaninful to the user.
So the method becames:

   get("/") {
     try {
      val title = db.get_collection("main", "api", "title")
      send_json(title)
     } catch {
      case e:java.util.NoSuchElementException => send_error("DB malformed", InternalError)
      case e:com.mongodb.MongoException => send_error("Can not connect to MongoDB", InternalError)
      case e => send_error("Internal Error", InternalError)
    }
  }

The try catch is bigger that the actual method and I need to do this for every method, the class become at first sight an ugly collection of try catch.
There is any way to avoid or minimize the bad looking and distracting try catch all over the code? I am new to Scala (and Java BTW) so I guess I am missing something.

I dont want the DB object to send JSON, so having the try catch on the db.get_collection method is not an option.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:59:34+00:00Added an answer on May 30, 2026 at 12:59 pm

    Well, I don’t know Scalatra enough, but the catch block is a partial function, so you could do something like this:

    val errorHandling: PartialFunction[Throwable, Unit] = {
      case e:java.util.NoSuchElementException => send_error("DB malformed", InternalError)
      case e:com.mongodb.MongoException => send_error("Can not connect to MongoDB", InternalError)
      case e => send_error("Internal Error", InternalError)
    }
    
    get("/") {
      try {
       val title = db.get_collection("main", "api", "title")
       send_json(title)
      } catch errorHandling
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
I'm developing a Scalatra web app with Scalate Jade and using sbt 0.11.0 I've
Using Rails 3.2.0 with haml and sass: I Would like to link an external
Using the HTML5 File API I can get the Binary String representation of a
Using Android TelephonyManager an application can obtain the state of data activity over the
Using Core Data, I have a fetch request to fetch the minimum of a
using file_get_contents , I open an Internet URL and get the contents of this
I'm using the scalatra-sbt-prototype . What would I have to modify, and where ,
Using the C# Facebook SDK 5.0.3 everything works fine whit the client.Get(/me). But when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.