map reduce, how do you add errors during map stage...
# suitescript
m
map reduce, how do you add errors during map stage for instance, so i can check them during the summarize stage? I mean, should i use the context.write function to add errors to what object?
m
thanks i could not find it in the doc
thank you
c
I previously had to go through the same learning journey 🙂
m
sorry to bother for silly stuff
@Craig i am reading how to process errors within the iterator but i cannot understand how to log them. I mean, let’s say i am processing data in my map function and want to log an error for one key because some important data is missing and then i want to iterate over the errors in map and send an email to the process owner to notify about this. Do i have to create my own error object in the context or i can use this “errors” list somehow?
c
log.error will log the error for you then you can use the iterator
m
got it
thanks
c
there is a summary object for each stage
that object will contain any errors that occurred in that stage
e.g. context.inputSummary will be available in the summarize() function
You can use the iterator to iterate through each inputSummary.error
m
yes i saw that but it was not clear how to actually log the errors
thank you
😒 still not working
let me recap,
I want to throw any error in the map function and capture those errors in the context.mapSummary.errors collection
so
I am processing only 1 record just for testing
in the map fucntion I am not using try and catch and only loggin an error like this:
log.error("map","my error")
in my summary
context.mapSummary.errors.iterator().each(function {key, error} { log.error("my key" + key, error); return true; });
i am chcking the log in the UI and see the error like another error but i dont see it processing the iterator in teh summary.
what am i missing?
b
mapSummary.errors
is for thrown errors
m
i also tried with throw “my error” in the map function
but still i cannot see it in the collection
b
and what did that code look like
your description so far makes it sound like you are throwing a string
m
yes
what should i type?
i had understood i could log.error() something in map and will be available inthe list
but it is not
b
normally you throw the object generated by N/error
m
so let me understand
i add the N/error dependency
and throw and error object
and then it should be availabel inthe iterator?
b
you can alternatively throw an object create by the Error constructor
either way, you throw an error in the map function
m
like
error.create({ name: "MY_ERROR_CODE", message: "my message", notifyOff: true });
b
m
yes using throw
i get that
my question was if the object should be of type N/error
b
you can throw whatever you want
but thats really unusual
m
this is my question in fact, maybe i am misunderstanding what the errors iterator has been designed for
b
to log things thrown during the map stage
m
my only objective is that if for some reason i determine that some values for a key are not valid, i want to log them and then at the summary do something like send an email or something
b
real scripts tend to do emails or creating custom records
m
this is why i thought that during the map function i could just do somehting like:
log.error("my error","value") or throw "my error"
and capture that on the summary
this is all i wanted
b
i personally dont see why you couldnt do that
m
if i do this:
function map(context) {
const tranId = context.key;
const tran = JSON.parse(context.value).values;
log.error("map", "test");
context.write(tranId, context.value);
}
b
thats a fail
still no throws
m
good
then i did throw
same code with throw “bla bla”
instead of log.error
and same result
i see the errors in the log UI but cannot capture them in the summary
b
you are going to need to share your code
m
here is the map
Copy code
function map(context) {
    
    const tranId = context.key;
    const tran = JSON.parse(context.value).values;

    throw "this is my error" 
    context.write(tranId, context.value);
  }
here is the summary
Copy code
function summarize(context) {
    
    log.audit("summarize", "entered");

    if (context.inputSummary.error) {
      log.error("Input Error", context.inputSummary.error);
    }

    context.mapSummary.errors.iterator().each(function (key, error) {
      log.error("Map Error for key: " + key, error);
      return true;
    });

    context.reduceSummary.errors.iterator().each(function (key, error) {
      log.error("Reduce Error for key: " + key, error);
      return true;
    });
  
  }
b
throw "this is my error"
that is not sane
throw an error
m
i know it is a test
i also tried with thsi
throw new Error(“test”)
and adding N/error, creating an error and throwing that error
in any case i see the error correctly logged in the UI but it is not added to the collection
b
what does the error look like in the ui?
m
depending the type of error but for the new Error says: TITLE: Map error for key xxxx, and in the DETAILS: {type: error.SuiteScriptError, name: Error, message: “error: test at OBject.map …”
b
i would prefer a screenshot to be sure
but that looks inline with with I would expect from your code
what are you expecting from it?
m
message has been deleted
all i want is to be able to parse the collection of map errors in the summary and process them (to send an email for instance)
b
the thing is, the code you shared so far only logs
your output so far is that it only logs
m
i dont understand i mean i am testing
how it works
i wanted to firstly understand how to ADD errors to the map.errors collection
this was my queston in the beginning maybe i am misunderstanding and all this does is to log errors that comes from the sysetm
b
the answer to that is to throw an error in the map phase
m
so if something happens during the map or reduce stage i can get a summary of those errors
but i cannot trigger them myself if i want to
it is what i did
b
and it worked
m
in the map phase i did that
it worked but it was not added to the iterator
tothe collection sorry
b
the log is the log from the code in the iterator function
m
if during the summary i iterate over the map.errors collection i cannot get this errori previously threw
oh
you mean that if i remvoe this line from the summary i wont see this error in the log?
b
its why i was confused with a lot of what you were saying
you were describing the proper functioning of your code as if it wasnt working
m
oh i see now
thank you i understand i thought i was looking at NS results but in fact this message is coming from the iterator
in any case the key part here is that only works with throws as you always mentioned
btw may i use try and catch in map or reduce functions or it si redundant?
b
either way you will need to implement code in the summarize to handle errors
m
ok
b
not all errors are catchable
m
thank you
yes i know
s
116 replies about why one might choose a simple scheduled script over MR 🙂
🤣 1