Custom Error View in Splunk Part 2: Converting Your Error to a JS Module
What is a Module?
So, the code we wrote originally is already in good shape, i.e.
When we define a new module, we do something like this instead:
Notice the similarity? That is because using both require and define follow the module pattern. This goes more specifically for AMD, which is more efficient and helps to ensure that all the dependencies are loaded before doing anything.
Time to Modularize Our Search Event
Inside of the same directory where your app.js and app.css files are, add a new folder called ‘components.’ This isn’t necessary, but it helps to organize our modules. Inside of the components folder create a new file called ErrorView.js
The first thing to do inside of ErrorView is to create a new module definition and also list it’s dependencies:
Next, we’ll define our function expression. We’ll assign ErrorView to our unnamed function:
We are going to pass two parameters to our ErrorView, the search and the visualization. Then, inside of our function we’ll just add in the on event we had used in part 1. The only change here is that instead of using table, I switched it to ‘viz’ to make it more generic. I did this because if we reuse this for multiple types of visualizations, someone else looking at this may find it to be misleading if we use the term ‘table’.
Now, back in our app.js file we will need to make a reference to the new component we created. We can then completely replace the on event we had previously with one line new ErrorView(search, table); and we’ll assign it to a variable called error. We then pass the search and table variables to our new module.
Restart Splunk and you should find that your error still works.
About Hurricane Labs
Hurricane Labs is a dynamic Managed Services Provider that unlocks the potential of Splunk and security for diverse enterprises across the United States. With a dedicated, Splunk-focused team and an emphasis on humanity and collaboration, we provide the skills, resources, and results to help make our customers’ lives easier.
For more information, visit www.hurricanelabs.com and follow us on Twitter @hurricanelabs.