diff --git a/numba/core/typing/templates.py b/numba/core/typing/templates.py
index 30082ce3b..33caa3257 100644
--- a/numba/core/typing/templates.py
+++ b/numba/core/typing/templates.py
@@ -285,6 +285,28 @@ class FunctionTemplate(ABC):
             key = key.im_func
         return key
 
+    @classmethod
+    def get_source_code_info(cls, impl):
+        """
+        Gets the source information about function impl.
+        Returns:
+
+        code - str: source code as a string
+        firstlineno - int: the first line number of the function impl
+        path - str: the path to file containing impl
+
+        if any of the above are not available something generic is returned
+        """
+        try:
+            code, firstlineno = inspect.getsourcelines(impl)
+        except OSError: # missing source, probably a string
+            code = "None available (built from string?)"
+            firstlineno = 0
+        path = inspect.getsourcefile(impl)
+        if path is None:
+            path = "<unknown> (built from string?)"
+        return code, firstlineno, path
+
     @abstractmethod
     def get_template_info(self):
         """
@@ -346,8 +368,8 @@ class AbstractTemplate(FunctionTemplate):
     def get_template_info(self):
         impl = getattr(self, "generic")
         basepath = os.path.dirname(os.path.dirname(numba.__file__))
-        code, firstlineno = inspect.getsourcelines(impl)
-        path = inspect.getsourcefile(impl)
+
+        code, firstlineno, path = self.get_source_code_info(impl)
         sig = str(utils.pysignature(impl))
         info = {
             'kind': "overload",
@@ -421,8 +443,7 @@ class CallableTemplate(FunctionTemplate):
     def get_template_info(self):
         impl = getattr(self, "generic")
         basepath = os.path.dirname(os.path.dirname(numba.__file__))
-        code, firstlineno = inspect.getsourcelines(impl)
-        path = inspect.getsourcefile(impl)
+        code, firstlineno, path = self.get_source_code_info(impl)
         sig = str(utils.pysignature(impl))
         info = {
             'kind': "overload",
@@ -730,7 +751,7 @@ class _OverloadFunctionTemplate(AbstractTemplate):
 
     @classmethod
     def get_source_info(cls):
-        """Return a dictionary with information about the source code  of the
+        """Return a dictionary with information about the source code of the
         implementation.
 
         Returns
@@ -751,8 +772,7 @@ class _OverloadFunctionTemplate(AbstractTemplate):
         """
         basepath = os.path.dirname(os.path.dirname(numba.__file__))
         impl = cls._overload_func
-        code, firstlineno = inspect.getsourcelines(impl)
-        path = inspect.getsourcefile(impl)
+        code, firstlineno, path = cls.get_source_code_info(impl)
         sig = str(utils.pysignature(impl))
         info = {
             'kind': "overload",
@@ -767,8 +787,7 @@ class _OverloadFunctionTemplate(AbstractTemplate):
     def get_template_info(self):
         basepath = os.path.dirname(os.path.dirname(numba.__file__))
         impl = self._overload_func
-        code, firstlineno = inspect.getsourcelines(impl)
-        path = inspect.getsourcefile(impl)
+        code, firstlineno, path = self.get_source_code_info(impl)
         sig = str(utils.pysignature(impl))
         info = {
             'kind': "overload",
@@ -836,8 +855,7 @@ class _IntrinsicTemplate(AbstractTemplate):
     def get_template_info(self):
         basepath = os.path.dirname(os.path.dirname(numba.__file__))
         impl = self._definition_func
-        code, firstlineno = inspect.getsourcelines(impl)
-        path = inspect.getsourcefile(impl)
+        code, firstlineno, path = self.get_source_code_info(impl)
         sig = str(utils.pysignature(impl))
         info = {
             'kind': "intrinsic",

Error hidden when source is not available
<!--

Thanks for opening an issue! To help the Numba team handle your information
efficiently, please first ensure that there is no other issue present that
already describes the issue you have
(search at https://github.com/numba/numba/issues?&q=is%3Aissue).

-->

## Reporting a bug

<!--

Before submitting a bug report please ensure that you can check off these boxes:

-->

- [x] I have tried using the latest released version of Numba (most recent is
 visible in the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
- [x] I have included below a minimal working reproducer (if you are unsure how
 to write one see http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports).


Example:

```python
import numpy as np

from numba import njit, typed, typeof
from numba.core import types
from numba.experimental import structref


@structref.register
class ParticleType(types.StructRef):
    pass


class Particle(structref.StructRefProxy):
    def __new__(cls, pos, mass):
        return structref.StructRefProxy.__new__(cls, pos) # <- didn't provide the required mass argument


structref.define_proxy(Particle, ParticleType, ["pos", "mass"])

Particle(pos=1, mass=2)
```
Because `StructRef`is building the source code dynamically, the `inspect.getsourcelines` will fail in https://github.com/numba/numba/blob/a7c121a6631ba576561bcc6e72a4bfe97151fb4d/numba/core/typing/templates.py#L752

It will be nice to fix this to make `StructRef` error be meaningful


diff --git a/numba/tests/test_errorhandling.py b/numba/tests/test_errorhandling.py
index fca546ccc..83f06211b 100644
--- a/numba/tests/test_errorhandling.py
+++ b/numba/tests/test_errorhandling.py
@@ -8,6 +8,7 @@ import os
 from numba import jit, njit, typed, int64, types
 from numba.core import errors
 import numba.core.typing.cffi_utils as cffi_support
+from numba.experimental import structref
 from numba.extending import (overload, intrinsic, overload_method,
                              overload_attribute)
 from numba.core.compiler import CompilerBase
@@ -423,6 +424,25 @@ class TestErrorMessages(unittest.TestCase):
         excstr = str(raises.exception)
         self.assertIn("Type Restricted Function in function 'unknown'", excstr)
 
+    def test_missing_source(self):
+
+        @structref.register
+        class ParticleType(types.StructRef):
+            pass
+
+        class Particle(structref.StructRefProxy):
+            def __new__(cls, pos, mass):
+                return structref.StructRefProxy.__new__(cls, pos)
+                # didn't provide the required mass argument ----^
+
+        structref.define_proxy(Particle, ParticleType, ["pos", "mass"])
+
+        with self.assertRaises(errors.TypingError) as raises:
+            Particle(pos=1, mass=2)
+
+        excstr = str(raises.exception)
+        self.assertIn("required positional argument: 'mass'", excstr)
+
 
 class TestDeveloperSpecificErrorMessages(SerialMixin, unittest.TestCase):
 

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest
apt-get update apt-get install -y gcc g++
3.7
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
certifi @ file:///croot/certifi_1671487769961/work/certifi
exceptiongroup==1.2.2
importlib-metadata==6.7.0
iniconfig==2.0.0
llvmlite==0.33.0
-e git+https://github.com/numba/numba.git@6bb29300254a40d2be35a6c88906bd4ddaad2c4d#egg=numba
numpy==1.21.6
packaging==24.0
pluggy==1.2.0
pytest==7.4.4
tomli==2.0.1
typing_extensions==4.7.1
zipp==3.15.0

name: numba
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=22.3.1=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - argparse==1.4.0
      - exceptiongroup==1.2.2
      - importlib-metadata==6.7.0
      - iniconfig==2.0.0
      - llvmlite==0.33.0
      - numpy==1.21.6
      - packaging==24.0
      - pluggy==1.2.0
      - pytest==7.4.4
      - tomli==2.0.1
      - typing-extensions==4.7.1
      - zipp==3.15.0
prefix: /opt/conda/envs/numba

numba/tests/test_errorhandling.py::TestErrorMessages::test_missing_source
numba/tests/test_errorhandling.py::TestErrorHandlingBeforeLowering::test_unsupported_make_function_return_inner_func numba/tests/test_errorhandling.py::TestUnsupportedReporting::test_unsupported_numpy_function numba/tests/test_errorhandling.py::TestMiscErrorHandling::test_commented_func_definition_is_not_a_definition numba/tests/test_errorhandling.py::TestMiscErrorHandling::test_handling_forgotten_numba_internal_import numba/tests/test_errorhandling.py::TestMiscErrorHandling::test_handling_of_write_to_reflected_global numba/tests/test_errorhandling.py::TestMiscErrorHandling::test_handling_of_write_to_typed_dict_global numba/tests/test_errorhandling.py::TestMiscErrorHandling::test_handling_undefined_variable numba/tests/test_errorhandling.py::TestMiscErrorHandling::test_handling_unsupported_generator_expression numba/tests/test_errorhandling.py::TestMiscErrorHandling::test_use_of_exception_for_flow_control numba/tests/test_errorhandling.py::TestMiscErrorHandling::test_use_of_ir_unknown_loc numba/tests/test_errorhandling.py::TestConstantInferenceErrorHandling::test_basic_error numba/tests/test_errorhandling.py::TestErrorMessages::test_abstract_template_source numba/tests/test_errorhandling.py::TestErrorMessages::test_callable_template_source numba/tests/test_errorhandling.py::TestErrorMessages::test_concrete_template_source numba/tests/test_errorhandling.py::TestErrorMessages::test_error_function_source_is_correct numba/tests/test_errorhandling.py::TestErrorMessages::test_external_function_pointer_template_source numba/tests/test_errorhandling.py::TestErrorMessages::test_intrinsic_template_source numba/tests/test_errorhandling.py::TestErrorMessages::test_no_match_error numba/tests/test_errorhandling.py::TestErrorMessages::test_overloadattribute_template_source numba/tests/test_errorhandling.py::TestErrorMessages::test_overloadfunction_template_source numba/tests/test_errorhandling.py::TestErrorMessages::test_overloadmethod_template_source numba/tests/test_errorhandling.py::TestErrorMessages::test_specific_error numba/tests/test_errorhandling.py::TestDeveloperSpecificErrorMessages::test_bound_function_error_string
numba/core/typing/templates.py
numba/core/typing/templates.py