Support returning 404 when file referenced by BodyAsFile isn't found #538

Closed
opened 2025-12-29 15:26:19 +01:00 by adam · 5 comments
Owner

Originally created by @goldsam on GitHub (Aug 1, 2023).

Originally assigned to: @StefH on GitHub.

Is your feature request related to a problem? Please describe.

Currently, a 500 error status is returned for a stub which uses BodyAsFile to reference a non-existent file.
it would be useful to specify a 404 status could be returned instead when this occurs.

Describe the solution you'd like

The easiest solution would be to add a flag to prevent the mock from faulting and return a 500 and instead let additional mocks be evaluated. Then, a 404 fallback stub could be specified which a priority that evaluates after the stub using BodyAsFile.

Describe alternatives you've considered

Additional additional fields to support an "error" case seems unnecessary and complicated and breaks the existing paradigm.

Is your feature request supported by WireMock (java version)? Please provide details.

Don't know.

Additional context

n/a

Originally created by @goldsam on GitHub (Aug 1, 2023). Originally assigned to: @StefH on GitHub. **Is your feature request related to a problem? Please describe.** Currently, a 500 error status is returned for a stub which uses `BodyAsFile` to reference a non-existent file. it would be useful to specify a 404 status could be returned instead when this occurs. **Describe the solution you'd like** The easiest solution would be to add a flag to prevent the mock from faulting and return a 500 and instead let additional mocks be evaluated. Then, a 404 fallback stub could be specified which a priority that evaluates after the stub using `BodyAsFile`. **Describe alternatives you've considered** Additional additional fields to support an "error" case seems unnecessary and complicated and breaks the existing paradigm. **Is your feature request supported by [WireMock (java version)](https://www.wiremock.org)? Please provide details.** Don't know. **Additional context** n/a
adam added the question label 2025-12-29 15:26:19 +01:00
adam closed this issue 2025-12-29 15:26:19 +01:00
Author
Owner

@StefH commented on GitHub (Aug 3, 2023):

@goldsam
I'll investigate the error.

About 404: in several places in the matchers or other code, I catch exceptions, and depending on https://github.com/WireMock-Net/WireMock.Net/blob/master/src/WireMock.Net/Matchers/IMatcher.cs#L21 I do throw an exception.
However I think I need to change this behavior, and just return the exception-message in the 404 message so that in case of an exception, the caller can figure out what could be wrong.

@StefH commented on GitHub (Aug 3, 2023): @goldsam I'll investigate the error. About `404`: in several places in the matchers or other code, I catch exceptions, and depending on https://github.com/WireMock-Net/WireMock.Net/blob/master/src/WireMock.Net/Matchers/IMatcher.cs#L21 I do throw an exception. However I think I need to change this behavior, and just return the exception-message in the 404 message so that in case of an exception, the caller can figure out what could be wrong.
Author
Owner

@goldsam commented on GitHub (Aug 4, 2023):

It might nice to allow evaluation to continue to the next stub, so that the user could then stub a 404 or any other behavior they might desire. Perhaps a flag in the stub to control this?

@goldsam commented on GitHub (Aug 4, 2023): It might nice to allow evaluation to continue to the next stub, so that the user could then stub a 404 or any other behavior they might desire. Perhaps a flag in the stub to control this?
Author
Owner

@StefH commented on GitHub (Aug 5, 2023):

Currently the response is indeed like this:
http status 500

{
    "Status": "Could not find file 'c:\\error.xml'."
}

The easiest solution would be to add a flag to prevent the mock from faulting and return a 500 and instead let additional mocks be evaluated. Then, a 404 fallback stub could be specified which a priority that evaluates after the stub using BodyAsFile.

In that case a proposal could be like:

{
    "Guid": "fd8ca21b-db82-48bc-ae5a-fc2153c2b0db",
    "Request": {
        "Path": {
            "Matchers": [
                {
                    "Name": "WildcardMatcher",
                    "Pattern": "/bodyasfile_transform123"
                }
            ]
        },
        "Methods": [
            "get"
        ]
    },
    "Response": {
        "StatusCode": 200,
        "Headers": { "Content-Type": "application/xml" },
        "BodyAsFile": "c:\\error.xml",
        "BodyAsFileIsCached": true,
    	"UseTransformer": true,
    	"UseTransformerForBodyAsFile": true
    },
    "ResponseWhenException": {
     "StatusCode": 404
    }
}

What are your thoughts?

@StefH commented on GitHub (Aug 5, 2023): Currently the response is indeed like this: **http status 500** ``` JSON { "Status": "Could not find file 'c:\\error.xml'." } ``` --- > The easiest solution would be to add a flag to prevent the mock from faulting and return a 500 and instead let additional mocks be evaluated. Then, a 404 fallback stub could be specified which a priority that evaluates after the stub using BodyAsFile. In that case a proposal could be like: ``` json { "Guid": "fd8ca21b-db82-48bc-ae5a-fc2153c2b0db", "Request": { "Path": { "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "/bodyasfile_transform123" } ] }, "Methods": [ "get" ] }, "Response": { "StatusCode": 200, "Headers": { "Content-Type": "application/xml" }, "BodyAsFile": "c:\\error.xml", "BodyAsFileIsCached": true, "UseTransformer": true, "UseTransformerForBodyAsFile": true }, "ResponseWhenException": { "StatusCode": 404 } } ``` What are your thoughts?
Author
Owner

@goldsam commented on GitHub (Aug 6, 2023):

That works for me, but I do think it might be adding more complexity than is necessary. You could also just add a SkipOnException flag instead and continue to the next stub when an exception occurs when set.

Either way, I appreciate you giving this your attention.

@goldsam commented on GitHub (Aug 6, 2023): That works for me, but I do think it might be adding more complexity than is necessary. You could also just add a `SkipOnException` flag instead and continue to the next stub when an exception occurs when set. Either way, I appreciate you giving this your attention.
Author
Owner

@StefH commented on GitHub (Aug 9, 2023):

In this case:

The mapping is invalid (e.g. due to invalid referenced file), which results that the output mapping cannot be constructed. In that case 500 is returned.

Also for example in XPathMatcher, I've change the code so that:

  • in case the input cannot be matched, the mapping is skipped (this is same as before)
  • in case the pattern (defined in the mapping) is invalid, the exception is logged, and the mapping gets a score = 0, which means that it's skipped.
@StefH commented on GitHub (Aug 9, 2023): In this case: The mapping is invalid (e.g. due to invalid referenced file), which results that the output mapping cannot be constructed. In that case 500 is returned. Also for example in XPathMatcher, I've change the code so that: - in case the input cannot be matched, the mapping is skipped (this is same as before) - in case the pattern (defined in the mapping) is invalid, the exception is logged, and the mapping gets a score = 0, which means that it's skipped.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#538