/**
 * @license
 * Copyright 2019 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.nanos.dig;

import foam.core.X;
import foam.lib.*;
import foam.lib.csv.CSVOutputter;
import foam.lib.json.JSONParser;
import foam.lib.json.Outputter;
import foam.lib.json.OutputterMode;
import foam.nanos.dig.exception.DigErrorMessage;
import foam.nanos.http.Format;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * DigUtil contains static utility methods for working with Dig and Sugar.
 */
public class DigUtil {
  /**
   * Output DigErrorMessage object to HTTP response.
   *
   * @param x The context.
   * @param error The error to output.
   * @param format The format of the output.
   */
  public static void outputException(X x, DigErrorMessage error, Format format) {
    HttpServletResponse resp  = x.get(HttpServletResponse.class);
    PrintWriter         out   = x.get(PrintWriter.class);

    resp.setStatus(Integer.parseInt(error.getStatus()));
    if ( format == Format.JSON ) {
      //output error in json format
      resp.setContentType("application/json");

      JSONParser jsonParser = new JSONParser();
      jsonParser.setX(x);
      Outputter outputterJson = new Outputter(x).setPropertyPredicate(new AndPropertyPredicate(x, new PropertyPredicate[] {new NetworkPropertyPredicate(), new PermissionedPropertyPredicate()}));
      outputterJson.setOutputDefaultValues(true);
      outputterJson.setOutputClassNames(true);
      outputterJson.output(error);
      out.println(outputterJson.toString());

    } else if ( format == Format.XML )  {
      //output error in xml format
      resp.setContentType("application/xml");

      foam.lib.xml.Outputter outputterXml = new foam.lib.xml.Outputter(OutputterMode.NETWORK);
      outputterXml.output(error);
      out.println(outputterXml.toString());

    } else if ( format == Format.CSV )  {
      //output error in csv format
      resp.setContentType("text/csv");

      CSVOutputter outputterCsv = new foam.lib.csv.CSVOutputterImpl.Builder(x).build();
      outputterCsv.outputFObject(x, error);
      out.println(outputterCsv.toString());

    } else if ( format == Format.HTML ) {
      //output error in html format
      resp.setContentType("text/html");

      foam.lib.html.Outputter outputterHtml = new foam.lib.html.Outputter(OutputterMode.NETWORK);
      outputterHtml.outputStartHtml();
      outputterHtml.outputStartTable();
      outputterHtml.outputHead(error);
      outputterHtml.put(error, null);
      outputterHtml.outputEndTable();
      outputterHtml.outputEndHtml();
      out.println(outputterHtml.toString());
    } else if ( format == Format.JSONJ ) {
      //output error in jsonJ format
      resp.setContentType("application/json");

      JSONParser jsonParser = new JSONParser();
      jsonParser.setX(x);
      Outputter outputterJson = new Outputter(x).setPropertyPredicate(new AndPropertyPredicate(new PropertyPredicate[] {new StoragePropertyPredicate(), new PermissionedPropertyPredicate()}));
      outputterJson.setOutputDefaultValues(true);
      outputterJson.setOutputClassNames(true);
      outputterJson.outputJSONJFObject(error);
      out.println(outputterJson.toString());

    } else {
      throw new UnsupportedOperationException(
        String.format("Output DIG exception in %s format is not supported.", format.getName()));
    }
  }
}

@@ -142,16 +142,42 @@
 s.dig;%0A%0A
+import foam.core.FObject;%0A
 import f
@@ -188,16 +188,16 @@
 core.X;%0A
-
 import f
@@ -864,24 +864,262 @@
 t format) %7B%0A
+    HttpServletResponse resp = x.get(HttpServletResponse.class);%0A%0A    resp.setStatus(Integer.parseInt(error.getStatus()));%0A    outputFObject(x, error, format);%0A  %7D%0A%0A  public static void outputFObject(X x, FObject object, Format format) %7B%0A
     HttpServ
@@ -1235,65 +1235,8 @@
 );%0A%0A
-    resp.setStatus(Integer.parseInt(error.getStatus()));%0A
     
@@ -1735,37 +1735,38 @@
 tterJson.output(
-error
+object
 );%0A      out.pri
@@ -2043,21 +2043,22 @@
 .output(
-error
+object
 );%0A     
@@ -2338,21 +2338,22 @@
 ject(x, 
-error
+object
 );%0A     
@@ -2719,21 +2719,22 @@
 putHead(
-error
+object
 );%0A     
@@ -2752,21 +2752,22 @@
 tml.put(
-error
+object
 , null);
@@ -3417,21 +3417,22 @@
 FObject(
-error
+object
 );%0A     
@@ -3566,21 +3566,15 @@
 put 
-DIG exception
+FObject
  in 

Add DigUtil.outputFObject