Skip to content

Commit

Permalink
Export hardcoded java errors into properties (#418)
Browse files Browse the repository at this point in the history
Export hardcoded java errors into properties
  • Loading branch information
mPokornyETM authored Dec 18, 2022
1 parent 95b825b commit 1e92c0c
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public String getFunctionName() {
@NonNull
@Override
public String getDisplayName() {
return "Lock shared resource";
return Messages.LockStep_displayName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ public void validate() {
*/
public static void validate(String resource, String label, int quantity) {
if (label != null && !label.isEmpty() && resource != null && !resource.isEmpty()) {
throw new IllegalArgumentException("Label and resource name cannot be specified simultaneously.");
throw new IllegalArgumentException(Messages.error_labelAndNameSpecified());
}
if (label != null && !LockableResourcesManager.get().isValidLabel( label ) ) {
throw new IllegalArgumentException("The label does not exist: " + label);
throw new IllegalArgumentException(Messages.error_labelDoesNotExist(label));
}
}

Expand All @@ -101,7 +101,7 @@ public static class DescriptorImpl extends Descriptor<LockStepResource> {
@NonNull
@Override
public String getDisplayName() {
return "Resource";
return Messages.LockStepResource_displayName();
}

@RequirePOST
Expand All @@ -124,13 +124,13 @@ public static FormValidation doCheckLabel(@QueryParameter String value,
String resourceLabel = Util.fixEmpty(value);
String resourceName = Util.fixEmpty(resource);
if (resourceLabel != null && resourceName != null) {
return FormValidation.error("Label and resource name cannot be specified simultaneously.");
return FormValidation.error(Messages.error_labelAndNameSpecified());
}
if ((resourceLabel == null) && (resourceName == null)) {
return FormValidation.error("Either label or resource name must be specified.");
return FormValidation.error(Messages.error_labelOrNameMustBeSpecified());
}
if (resourceLabel != null && !LockableResourcesManager.get().isValidLabel(resourceLabel)) {
return FormValidation.error("The label does not exist: " + resourceLabel);
return FormValidation.error(Messages.error_labelDoesNotExist(resourceLabel));
}
return FormValidation.ok();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ public static class DescriptorImpl extends Descriptor<LockableResource> {
@NonNull
@Override
public String getDisplayName() {
return "Resource";
return Messages.LockableResource_displayName();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ public synchronized void unreserve(List<LockableResource> resources) {
@NonNull
@Override
public String getDisplayName() {
return "External Resources";
return Messages.LockableResourcesManager_displayName();
}

public synchronized void reset(List<LockableResource> resources) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static class DescriptorImpl extends JobPropertyDescriptor {
@NonNull
@Override
public String getDisplayName() {
return "Required Lockable Resources";
return Messages.RequiredResourcesProperty_displayName();
}

@Override
Expand Down Expand Up @@ -167,8 +167,7 @@ public FormValidation doCheckResourceNames(@QueryParameter String value,
if (names == null) {
return FormValidation.ok();
} else if (labelVal != null || script) {
return FormValidation.error(
"Only label, groovy expression, or resources can be defined, not more than one.");
return FormValidation.error(Messages.error_labelAndNameOrGroovySpecified());
} else {
List<String> wrongNames = new ArrayList<>();
for (String name : names.split("\\s+")) {
Expand All @@ -187,8 +186,7 @@ public FormValidation doCheckResourceNames(@QueryParameter String value,
return FormValidation.ok();
} else {
return FormValidation
.error("The following resources do not exist: "
+ wrongNames);
.error(Messages.error_resourceDoesNotExist(wrongNames));
}
}
}
Expand All @@ -209,13 +207,13 @@ public FormValidation doCheckLabelName(
return FormValidation.ok();
} else if (names != null || script) {
return FormValidation.error(
"Only label, groovy expression, or resources can be defined, not more than one.");
Messages.error_labelAndNameOrGroovySpecified());
} else {
if (LockableResourcesManager.get().isValidLabel(label)) {
return FormValidation.ok();
} else {
return FormValidation.error(
"The label does not exist: " + label);
Messages.error_labelDoesNotExist(label));
}
}
}
Expand Down Expand Up @@ -243,7 +241,7 @@ public FormValidation doCheckResourceNumber(@QueryParameter String value,
numAsInt = Integer.parseInt(number);
} catch(NumberFormatException e) {
return FormValidation.error(
"Could not parse the given value as integer.");
Messages.error_couldNotParseToint());
}
int numResources = 0;
if (names != null) {
Expand All @@ -254,7 +252,7 @@ public FormValidation doCheckResourceNumber(@QueryParameter String value,

if (numResources < numAsInt) {
return FormValidation.error(String.format(
"Given amount %d is greater than amount of resources: %d.",
Messages.error_givenAmountIsGreaterThatResurcesAmount(),
numAsInt,
numResources));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public void doUnlock(StaplerRequest req, StaplerResponse rsp)
String name = req.getParameter("resource");
LockableResource r = LockableResourcesManager.get().fromName(name);
if (r == null) {
rsp.sendError(404, "Resource not found " + name);
rsp.sendError(404, Messages.error_resourceDoesNotExist(name));
return;
}

Expand All @@ -191,7 +191,7 @@ public void doReserve(StaplerRequest req, StaplerResponse rsp)
String name = req.getParameter("resource");
LockableResource r = LockableResourcesManager.get().fromName(name);
if (r == null) {
rsp.sendError(404, "Resource not found " + name);
rsp.sendError(404, Messages.error_resourceDoesNotExist(name));
return;
}

Expand All @@ -200,7 +200,7 @@ public void doReserve(StaplerRequest req, StaplerResponse rsp)
String userName = getUserName();
if (userName != null) {
if (!LockableResourcesManager.get().reserve(resources, userName)) {
rsp.sendError(423, "Resource '" + name + "' already reserved or locked!");
rsp.sendError(423, Messages.error_resourceAlreadyLocked(name));
return;
}
}
Expand All @@ -216,7 +216,7 @@ public void doSteal(StaplerRequest req, StaplerResponse rsp)
String name = req.getParameter("resource");
LockableResource r = LockableResourcesManager.get().fromName(name);
if (r == null) {
rsp.sendError(404, "Resource not found " + name);
rsp.sendError(404, Messages.error_resourceDoesNotExist(name));
return;
}

Expand Down Expand Up @@ -246,7 +246,7 @@ public void doReassign(StaplerRequest req, StaplerResponse rsp)
String name = req.getParameter("resource");
LockableResource r = LockableResourcesManager.get().fromName(name);
if (r == null) {
rsp.sendError(404, "Resource not found " + name);
rsp.sendError(404, Messages.error_resourceDoesNotExist(name));
return;
}

Expand Down Expand Up @@ -274,7 +274,7 @@ public void doUnreserve(StaplerRequest req, StaplerResponse rsp)
String name = req.getParameter("resource");
LockableResource r = LockableResourcesManager.get().fromName(name);
if (r == null) {
rsp.sendError(404, "Resource not found " + name);
rsp.sendError(404, Messages.error_resourceDoesNotExist(name));
return;
}

Expand Down Expand Up @@ -302,7 +302,7 @@ public void doReset(StaplerRequest req, StaplerResponse rsp)
String name = req.getParameter("resource");
LockableResource r = LockableResourcesManager.get().fromName(name);
if (r == null) {
rsp.sendError(404, "Resource not found " + name);
rsp.sendError(404, Messages.error_resourceDoesNotExist(name));
return;
}

Expand All @@ -325,7 +325,7 @@ public void doSaveNote(final StaplerRequest req, final StaplerResponse rsp)

final LockableResource resource = getResource(resourceName);
if (resource == null) {
rsp.sendError(404, "Resource not found: '" + resourceName + "'!");
rsp.sendError(404, Messages.error_resourceDoesNotExist(resourceName));
} else {
String resourceNote = req.getParameter("note");
if (resourceNote == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,18 @@ LockableResourcesRootAction.StealPermission.Description=This permission grants t
LockableResourcesRootAction.ViewPermission=View
LockableResourcesRootAction.ViewPermission.Description=This permission grants the ability to view \
lockable resources.

# Java errors
error.labelDoesNotExist=The resource label does not exist: {0}.
error.resourceDoesNotExist=The resource does not exist: {0}.
error.labelOrNameMustBeSpecified=Either resource label or resource name must be specified.
error.labelAndNameSpecified=Resource label and resource name cannot be specified simultaneously.
error.labelAndNameOrGroovySpecified=Only resource label, groovy expression, or resource names can be defined, not more than one.
error.couldNotParseToint=Could not parse the given value as integer.
error.givenAmountIsGreaterThatResurcesAmount=Given amount %d is greater than amount of resources: %d.
error.resourceAlreadyLocked=Resource {0} already reserved or locked!
# display-names
LockStep.displayName=Lock shared resource
LockStepResource.displayName=Resource
LockableResource.displayName=Resource
LockableResourcesManager.displayName=External Resources
RequiredResourcesProperty.displayName=Required Lockable Resources
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,7 @@ public void lockWithInvalidLabel() throws Exception {
WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
j.waitForCompletion(b1);
j.assertBuildStatus(Result.FAILURE, b1);
j.assertLogContains("The label does not exist: invalidLabel", b1);
j.assertLogContains("The resource label does not exist: invalidLabel", b1);
isPaused(b1, 0, 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ public void validationFailure() throws Exception {
r.setLabels("some-label");

assertEquals(
"Only label, groovy expression, or resources can be defined, not more than one.",
"Only resource label, groovy expression, or resource names can be defined, not more than one.",
d.doCheckResourceNames("resource1", null, true, null).getMessage());
assertEquals(
"Only label, groovy expression, or resources can be defined, not more than one.",
"Only resource label, groovy expression, or resource names can be defined, not more than one.",
d.doCheckResourceNames("resource1", "some-label", false, null).getMessage());
assertEquals(
"Only label, groovy expression, or resources can be defined, not more than one.",
"Only resource label, groovy expression, or resource names can be defined, not more than one.",
d.doCheckResourceNames("resource1", "some-label", true, null).getMessage());
assertEquals(
"Only label, groovy expression, or resources can be defined, not more than one.",
"Only resource label, groovy expression, or resource names can be defined, not more than one.",
d.doCheckLabelName("some-label", "resource1", false, null).getMessage());
assertEquals(
"Only label, groovy expression, or resources can be defined, not more than one.",
"Only resource label, groovy expression, or resource names can be defined, not more than one.",
d.doCheckLabelName("some-label", null, true, null).getMessage());
assertEquals(
"Only label, groovy expression, or resources can be defined, not more than one.",
"Only resource label, groovy expression, or resource names can be defined, not more than one.",
d.doCheckLabelName("some-label", "resource1", true, null).getMessage());

assertEquals(FormValidation.ok(), d.doCheckResourceNames("resource1", null, false, null));
Expand Down

0 comments on commit 1e92c0c

Please sign in to comment.