A real valued function defined on a Borel subset of $\mathbb{R}$ with a countable number of discontinuities is Borel measurable I have proved the following statement and I would like to know if my proof is correct and/or/if/how it can be improved, thanks.
"Suppose $X$ is a Borel subset of $\mathbb{R}$ and $f:X\to\mathbb{R}$ is a function such that $\{x\in X: f\text{ is not continuous at }x\}$ is a countable set. Prove $f$ is a Borel measurable function."
My proof:
(EDIT: my proof is wrong in the case $\{x\in X: f\text{ is not continuous at }x\}$ is a countable set (a counterexample, as pointed out by Ramiro is the Thomae function) but it should work in the case $\{x\in X: f\text{ is not continuous at }x\}$ is finite)
Let $d_i, i\geq 1$ denote the points at which $f$ is discontinuous and fix $a\in\mathbb{R}$; if $x\in X$ and $f(x)>a$ then either $x=d_i$ for some $i\geq 1$ or $f$ is continuous at $x$ so if we set $\delta_x:=\frac{1}{2}\cdot\inf\{|x-d_i|:i\geq 1\}$ we have $f((x-\delta_x,x+\delta_x)\cap X)\subseteq (a,+\infty)$ (equivalently $(x-\delta_x,x+\delta_x)\cap X\subseteq f^{-1}((a,+\infty))$ being $f$ continuous in this whole set so $f^{-1}((a,+\infty))=\bigcup_{x\in f^{1}((a,+\infty)), x\neq d_i\forall i\geq 1} (x-\delta_x,x+\delta_x)\cap X\cup \{d_i:f(d_i)>a\}$ which being the union of two Borel sets ($(x-\delta_x,x+\delta_x)$ is an open subset of $\mathbb{R}$ so it is Borel, $X$ is a Borel set by hypothesis so their intersection is Borel too and so is also their union and $\{d_i:f(d_i)>a\}$ is countable hence Borel too) is Borel. So, by LEMMA, we can conclude that $f$ is Borel-measurable, as desired.

LEMMA. Suppose $(X,\mathcal{S})$ is a measurable space and $f:X\to\mathbb{R}$ is a function such that $f^{-1}((a,+\infty))\in\mathcal{S}$ for all $\in\mathbb{R}$ then $f$ is an $\mathcal{S}$-measurable function.
DEF. (measurable function) Suppose $(X,\mathcal{S})$ is a measurable space. A function $f:X\to\mathbb{R}$ is called $\mathcal{S}$-measurable if $f^{-1}(B)\in\mathcal{S}$ for every Borel set $B\subset\mathbb{R}$.
DEF. (Borel-measurable function) Suppose $X\subset\mathbb{R}$. A function $f:X\to\mathbb{R}$ is called Borel measurable if $f^{-1}(B)$ is a Borel set for every Borel set $B\subset\mathbb{R}$.
 A: In fact, your proof is not correct. The issue is that $\delta_x$ may be $0$. In fact, there is a function that is discontinuous on all the rational numbers and continuous on all irrational number (see here).
However, the some ideas in your proof are fine. Here is how they are developed into a correct proof:
Suppose $X$ is a Borel subset of $\mathbb{R}$ and $f:X\to\mathbb{R}$ is a function such that $\{x\in X: f\text{ is not continuous at }x\}$ is a countable set.
Let $D= \{x\in X: f\text{ is not continuous at }x\}$. Since $D$ is countable, we have that $D$ is a Borel subset. So $E=X\setminus D$ is a Borel subset and $f|_E: E \to \mathbb{R}$ is continuous, so $f|_E$ is a Borel measurable function.
It means that, for all $B \subseteq \mathbb{R}$ such that $B$ is Borel measurable, then $f|_E^{-1}(B)$ is Borel measurable. Now, since $X=E \cup D$, we have
$$f^{-1}(B) = (f^{-1}(B) \cap E) \cup (f^{-1}(B)\cap D)= f|_E^{-1}(B) \cup (f^{-1}(B)\cap D)$$
We know that $f|_E^{-1}(B)$ is Borel measurable and that $f^{-1}(B)\cap D$ is countable (and so Borel measurable). It follows that  $f^{-1}(B)$ is Borel measurable.
So, we have proved that,  for all $B \subseteq \mathbb{R}$ such that $B$ is Borel measurable, $f^{-1}(B)$ is Borel measurable.  It means that $f$ is a Borel measurable function.
