How can I return a JSON response from an internal function ? (not the main callback)
You can do it by checking is $this-&gt;someCalculation() is instance of JsonResponse. It is is we return the response, otherwise we set $result to the calculation result.

private function someCalculation() {
  try {
    // Something.
  } catch ($e) {
    return new JsonResponse([
      'data' =&gt; NULL,
      'error' =&gt; $e-&gt;getMessage(),
    ]);
  }
}

public function getTemplate(Request $request) {
  // Alternatively you can return an array with a status code and check on that instead.
  if ($this-&gt;someCalculation() instanceof JsonResponse) {
    return $this-&gt;someCalculation();
  }
  // If it didn't fail set set $result to the calculation result.
  $result = $this-&gt;someCalculation();

  return new JsonResponse([
    'data' =&gt; $result,
  ]);
}