Throwing Error Message in Meteor


#1

In my application I have a problem in displaying the error message. I have a function, in which file uploaded is converted from one file format to another. It should throw the message if the file is correctly converted or not.
Following is how I am throwing the message. I don’t want to use console.log. I want to throw Error/alert.

if (error) {
throw (new Meteor.Error(“There’s some error in converting file”));
} else {
console.log(“File has been converted”);
}

Also, I have posted the question on stackoverflow, but unable to get any help Throwing Error. Please help me out.


#2

Meteor.Error is a server-side way of sending a thrown error over DDP to the client. Most often it is used within the context of a call to a method:

On the server:

Meteor.methods({
  myMethod: function(params) {
  // do stuff
  ...
  // something bad happened: throw a Meteor.error:
  throw new Meteor.Error('bad', 'stuff happened');
});

and on the client:

Meteor.call('myMethod', params, function(error, result) {
  if (error) {
    // This is where we see that thrown Meteor.Error from the method
  } else {
   // Do stuff with result
  }
});

If you want a conventionally thrown error, use the standard JavaScript throw new Error()


#3

I am trying the same way but unable to get the message. I am adding the code for both the client and the server side. Sorry, for adding too much of the code but I am unable to figure out where I am commiting the mistake. Please help and tell where I am going wrong. Here is the code Test


#4

At line 36 you are passing the error reason as the error code.

EDIT: Also at line 56 the error code should be a string

More info: http://docs.meteor.com/#/full/meteor_error


#5

@TwinTails I have followed as per the documentation and as per your suggestions (line 36) I have edited the code too. Following is the code, and console o/p. It shows the error on console but no error is shown, and follows the next steps.

Server

if (error) {
   throw new Meteor.Error("logg", "truth");
       } else {
   console.log("File has been converted" + objects[i] + i);

Client

Meteor.call('convertFile', function (error) {
    if (error.error === "logg") {
        alert("Error");
    };
});

Terminal

I20150623-00:25:43.007(5.5)? Exception in callback of async function:
Error: truth [logg]
I20150623-00:25:43.007(5.5)?     at app/server/cfs_uploader.js:80:15
I20150623-00:25:43.007(5.5)?     at runWithEnvironment
(packages/meteor/dynamics_nodejs.js:108:1)
I20150623-00:25:43.007(5.5)? Exception in callback of async function:
Error: truth [logg]

#6

Can’t see anything wrong with that code now - what do you get if you add

console.log("Error obj: ", error);

above this line if (error.error === "logg") { ?


#7

@TwinTails Here is the output

I20150623-03:17:22.018(5.5)? at app/server/cfs_uploader.js:81:15
I20150623-03:17:22.018(5.5)? at runWithEnvironment (packages/meteor/dynamics_nodejs.js:108:1)
I20150623-03:17:22.019(5.5)? Error obj: { [Error: Command failed: db5_get_raw_internal_ptr() bad magic2 – database has become corrupted.
I20150623-03:17:22.019(5.5)? expected x35, got xdd
I20150623-03:17:22.019(5.5)? db_dirbuild(/home/deepak/Sagittarius/OGV-meteor/OGV/.meteor/local/cfs/files/modelFiles/modelFiles-BoKvde9s5i9z6gHhk-brlcad.g): db5_scan() failed
I20150623-03:17:22.019(5.5)?
I20150623-03:17:22.019(5.5)? db_dirbuild failed
I20150623-03:17:22.019(5.5)?
I20150623-03:17:22.019(5.5)? ] killed: false, code: 1, signal: null }
I20150623-03:17:22.019(5.5)? Exception in callback of async function: Error: truth [logg]


#8

The log code should go in the client, in the Meteor.call callback.


#9

@TwinTails Getting this every-time

I20150623-04:38:34.642(5.5)? Exception in callback of async function: Error: trust [logg]
I20150623-04:38:34.642(5.5)?     at app/server/cfs_uploader.js:80:15
I20150623-04:38:34.642(5.5)?     at runWithEnvironment (packages/meteor/dynamics_nodejs.js:108:1)
I20150623-04:38:34.642(5.5)? Exception in callback of async function: Error: trust [logg]
I20150623-04:38:34.642(5.5)?     at app/server/cfs_uploader.js:80:15
I20150623-04:38:34.642(5.5)?     at runWithEnvironment (packages/meteor/dynamics_nodejs.js:108:1)

Seems like Meteor.call not working :pensive:

I tried calling Meteor.call s shown but no luck

Template.cfsUploader.events({
     'change #fileInput': function(event, temp)
    {
	 	Meteor.call('convertFile', function (error) {
		console.log("Error obj: ", error);
		if (error.error === "logg") {
			alert("Error");
		} else {
			uploadFile(event, temp);
		}
	});
    }
});

#10

The exceptions are being reported from cfs_uploader, which is not shown in your code examples. Is it possible you’re looking in the wrong place?


#11

just a detail, you are missing fileID argument in meteor.call after you moved it around.


#12

Yes, these are the correct files :frowning:


#13

I have passed fileID. This is the updated code. If I pass fileID in Meteor.call somewhere else in the same function it says fileId not defined.