package com.hubspot.jinjava.interpret;

import com.google.common.collect.ImmutableList;
import java.util.Collections;
import java.util.List;

public class RenderResult {
  private final String output;
  private final Context context;
  private final List<TemplateError> errors;

  public RenderResult(String output, Context context, List<TemplateError> errors) {
    this.output = output;
    this.context = context;
    this.errors = errors;
  }

  public RenderResult(
    TemplateError fromException,
    Context context,
    List<TemplateError> errors
  ) {
    this.output = "";
    this.context = context;
    this.errors =
      ImmutableList.<TemplateError>builder().add(fromException).addAll(errors).build();
  }

  public RenderResult(String result) {
    this.output = result;
    this.context = null;
    this.errors = Collections.emptyList();
  }

  public boolean hasErrors() {
    return !errors.isEmpty();
  }

  public List<TemplateError> getErrors() {
    return errors;
  }

  public Context getContext() {
    return context;
  }

  public String getOutput() {
    return output;
  }

  public RenderResult withOutput(String newOutput) {
    return new RenderResult(newOutput, getContext(), getErrors());
  }
}

@@ -133,16 +133,43 @@
 il.List;
+%0Aimport java.util.Optional;
 %0A%0Apublic
@@ -669,16 +669,25 @@
 ableList
+%0A        
 .%3CTempla
@@ -703,16 +703,25 @@
 uilder()
+%0A        
 .add(fro
@@ -735,23 +735,94 @@
 ion)
-.addAll(errors)
+%0A        .addAll(Optional.ofNullable(errors).orElse(Collections.emptyList()))%0A        
 .bui

check for null errors in RenderResult