# Resolving a FancyVerb error when making a new code environment

full MWE presented at the end

I am creating my own code environment with minted for a text I am writing. It is enumerable, as well as reference-able. Everything works fine and the document compiles as "expected". Unfortunately there is an error that keeps popping up, even though the document compiles.

To create my code environment I use:

\newcounter{code}[chapter]
\renewcommand\thecode{\thechapter.\arabic{code}}
\definecolor{bg}{rgb}{0.975,0.95,1.0}

\newenvironment{code}[3][h!bp]
{\VerbatimEnvironment \refstepcounter{code}
\label{#3}
\begin{listing}[#1]
\textbf{Code~\thecode} #2
\centering
\begin{minted}[fontsize=\small, bgcolor=bg, frame=lines, framesep=2mm]{julia}
}
{\end{minted}\end{listing}}


and I call it like


\begin{code}{Example code defining the Lorenz-63 system, eq.5 in Julia, and obtaining a trajectory for it using DynamicalSystems.jl.}{DSY_cod_example}
using DynamicalSystems # load the library

# define dynamical rule for Lorenz-63
function lorenz_f(u, p, t)
σ = p[1]; ρ = p[2]; β = p[3]
du1 = σ*(u[2]-u[1])
du2 = u[1]*(ρ-u[3]) - u[2]
du3 = u[1]*u[2] - β*u[3]
return SVector{3}(du1, du2, du3)
end
tr = trajectory(lorenz, T; dt = dt)
\end{code}


which compiles fine to

but I am getting an error:

which points to the point in my code that I start the \begin{code} block. Unfortunately I do not understand the problem...

How do I stop this error? In my real document I have dozens of these code blocks and the errors cumulate...

FULL MWE

\documentclass{book}
\usepackage[utf8]{inputenc}

\title{codetest}
\author{test}
\date{January 2020}

\usepackage{natbib}
\usepackage{graphicx}
%% The following are for using code snippets
\usepackage{minted}
\usepackage{fontspec}
\usepackage{amsmath}
\usepackage{fancyvrb} % for inline code
\usepackage{listings}

\begin{document}

\maketitle

\newcounter{code}[chapter]
\renewcommand\thecode{\thechapter.\arabic{code}}
\definecolor{bg}{rgb}{0.975,0.95,1.0}

\newcommand{\incode}[1]{\colorbox{bg}{\texttt{#1}}}

\newenvironment{code}[3][h!bp]
{\VerbatimEnvironment \refstepcounter{code}
\label{#3}
\begin{listing}[#1]
\textbf{Code~\thecode} #2
\centering
\begin{minted}[fontsize=\small, bgcolor=bg, frame=lines, framesep=2mm]{julia}
}
{\end{minted}\end{listing}}

\chapter{Introduction}
Look at Code.~\ref{DSY_cod_example}, similar to the code that we will be showing in the rest of the book.

\begin{code}{Example code defining the Lorenz-63 system, eq.5 in Julia, and obtaining a trajectory for it using DynamicalSystems.jl.}{DSY_cod_example}
using DynamicalSystems # load the library

# define dynamical rule for Lorenz-63
function lorenz_f(u, p, t)
σ = p[1]; ρ = p[2]; β = p[3]
du1 = σ*(u[2]-u[1])
du2 = u[1]*(ρ-u[3]) - u[2]
du3 = u[1]*u[2] - β*u[3]
return SVector{3}(du1, du2, du3)
end
tr = trajectory(lorenz, T; dt = dt)
\end{code}

\bibliographystyle{plain}
\bibliography{references}
\end{document}


Unprotected end-of-lines.

\documentclass{book}
\usepackage[utf8]{inputenc}

\title{codetest}
\author{test}
\date{January 2020}

\usepackage{natbib}
\usepackage{graphicx}
%% The following are for using code snippets
\usepackage{minted}
\usepackage{fontspec}
\usepackage{amsmath}
\usepackage{fancyvrb} % for inline code
\usepackage{listings}

\begin{document}

\maketitle

\newcounter{code}[chapter]
\renewcommand\thecode{\thechapter.\arabic{code}}
\definecolor{bg}{rgb}{0.975,0.95,1.0}

\newcommand{\incode}[1]{\colorbox{bg}{\texttt{#1}}}

\newenvironment{code}[3][h!bp]
{\VerbatimEnvironment \refstepcounter{code}%
\label{#3}%
\begin{listing}[#1]%
\textbf{Code~\thecode} #2%
\centering
\begin{minted}[fontsize=\small, bgcolor=bg, frame=lines, framesep=2mm]{julia}%
}
{\end{minted}\end{listing}}

\chapter{Introduction}
Look at Code.~\ref{DSY_cod_example}, similar to the code that we will be showing in the rest of the book.

\begin{code}{Example code defining the Lorenz-63 system, eq.5 in Julia, and obtaining a trajectory for it using DynamicalSystems.jl.}{DSY_cod_example}
using DynamicalSystems # load the library

# define dynamical rule for Lorenz-63
function lorenz_f(u, p, t)
σ = p[1]; ρ = p[2]; β = p[3]
du1 = σ*(u[2]-u[1])
du2 = u[1]*(ρ-u[3]) - u[2]
du3 = u[1]*u[2] - β*u[3]
return SVector{3}(du1, du2, du3)
end
tr = trajectory(lorenz, T; dt = dt)
\end{code}

\bibliographystyle{plain}
\bibliography{references}
\end{document}