{"record":{"id":"df4feb5913f4aa3e","repo":"eclipse-vertx/vert.x","slug":"code-statuscode-expected-0","errorCode":null,"errorMessage":"code: <statusCode> (expected: 0+)","messagePattern":"code: <statusCode> \\(expected: 0\\+\\)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"vertx-core/src/main/java/io/vertx/core/http/impl/HttpServerResponseImpl.java","lineNumber":145,"sourceCode":"      if (handler != null) {\n        checkValid();\n      }\n      exceptionHandler = handler;\n      return this;\n    }\n  }\n\n  @Override\n  public int getStatusCode() {\n    synchronized (conn) {\n      return status.code();\n    }\n  }\n\n  @Override\n  public HttpServerResponse setStatusCode(int statusCode) {\n    if (statusCode < 0) {\n      throw new IllegalArgumentException(\"code: \" + statusCode + \" (expected: 0+)\");\n    }\n    synchronized (conn) {\n      checkHeadWritten();\n      this.status = HttpResponseStatus.valueOf(statusCode);\n      return this;\n    }\n  }\n\n  @Override\n  public String getStatusMessage() {\n    synchronized (conn) {\n      if (statusMessage == null) {\n        return status.reasonPhrase();\n      }\n      return statusMessage;\n    }\n  }\n","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/eclipse-vertx/vert.x/blob/fb308bd8c3f12c79f4ae89bef67fadf6c80d036e/vertx-core/src/main/java/io/vertx/core/http/impl/HttpServerResponseImpl.java#L127-L163","documentation":"setStatusCode validates that the given HTTP status code is non-negative (Netty-style precondition). Negative values cannot map to an HttpResponseStatus, so an IllegalArgumentException with the message 'code: <n> (expected: 0+)' is thrown before any state is changed.","triggerScenarios":"Calling response.setStatusCode(negativeNumber), typically from a variable holding an unvalidated error code or an off-by-one/arithmetic result.","commonSituations":"Mapping internal error codes (e.g. -1 for 'unknown') directly to the response status; computed status values from configuration; int defaults of -1 used as 'unset'.","solutions":["Validate the status code before calling setStatusCode, clamping or defaulting invalid values (e.g. to 500)","Replace sentinel negative values with a proper Optional/constant and map to a real HTTP status","Only pass literals from the valid HTTP status range (100-599 in practice)"],"exampleFix":"// before\nint code = errorToStatusCode(err); // may return -1\nresponse.setStatusCode(code); // IllegalArgumentException\n\n// after\nint code = errorToStatusCode(err);\nresponse.setStatusCode(code >= 0 ? code : 500);","handlingStrategy":"validation","validationCode":"if (statusCode < 0) {\n  statusCode = 500;\n}\nresponse.setStatusCode(statusCode);","typeGuard":null,"tryCatchPattern":"try {\n  response.setStatusCode(code);\n} catch (IllegalArgumentException e) {\n  response.setStatusCode(500);\n}","preventionTips":["Never map sentinel negative error codes directly to HTTP statuses","Validate computed status codes before assignment","Use an enum or constants for valid statuses"],"tags":["http","status-code","illegal-argument","validation"],"backgroundTag":"value-out-of-range","analyzedSha":"fb308bd8c3f12c79f4ae89bef67fadf6c80d036e","analyzedAt":"2026-09-06T11:37:12.241Z","contentChangedAt":"2026-09-06T11:37:12.241Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}