Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
Improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewprenger committed Dec 16, 2015
1 parent 9c547f2 commit 5d7affe
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 38 deletions.
28 changes: 14 additions & 14 deletions src/main/groovy/com/matthewprenger/cursegradle/CurseArtifact.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ class CurseArtifact implements Serializable {
/**
* Validate this artifact
*/
void validate() {
check(artifact != null, "artifact not configured")
check(changelogType != null, "changelogType was set to null")
check(changelog != null, "changelog not set")
check(releaseType != null, "releaseType not set")
check(CurseGradlePlugin.VALID_RELEASE_TYPES.contains(releaseType), "$releaseType is not a valid release type. Valid options are: $CurseGradlePlugin.VALID_RELEASE_TYPES")
curseRelations.each { it.validate() }
void validate(final def id) {
check(artifact != null, "Artifact not configured for project $id")
check(changelogType != null, "The changelogType was null for project $id")
check(changelog != null, "The changelog was not set for project $id")
check(releaseType != null, "The releaseType was not set for project $id")
check(CurseGradlePlugin.VALID_RELEASE_TYPES.contains(releaseType), "Invalid release type ($releaseType) for project $id. Valid options are: $CurseGradlePlugin.VALID_RELEASE_TYPES")
curseRelations.each { it.validate(id) }
}

/**
Expand All @@ -95,12 +95,12 @@ class CurseArtifact implements Serializable {
@Override
public String toString() {
return "CurseArtifact{" +
"artifact=" + artifact +
", changelogType=" + changelogType +
", changelog=" + changelog +
", displayName=" + displayName +
", releaseType='" + releaseType + '\'' +
", gameVersionStrings=" + gameVersionStrings +
'}';
"artifact=" + artifact +
", changelogType=" + changelogType +
", changelog=" + changelog +
", displayName=" + displayName +
", releaseType='" + releaseType + '\'' +
", gameVersionStrings=" + gameVersionStrings +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.matthewprenger.cursegradle

import com.google.common.base.Strings
import org.gradle.api.DefaultTask
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand Down Expand Up @@ -38,9 +39,7 @@ class CurseGradlePlugin implements Plugin<Project> {

extension.curseProjects.each { curseProject ->

if (curseProject.id == null || ''.equals(curseProject.id)) {
throw new RuntimeException("A CurseForge project was configured with a null or empty id")
}
Util.check(!Strings.isNullOrEmpty(curseProject.id), "A CurseForge project was configured without an id")

CurseUploadTask uploadTask = project.tasks.create("curseforge$curseProject.id", CurseUploadTask)
curseProject.uploadTask = uploadTask
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ class CurseProject {
*/
void validate() {
check(id != null, "Project id not set")
check(apiKey != null, "apiKey not set for project: $id")
check(mainArtifact != null, "mainArtifact not set for project: $id")
check(!gameVersionStrings.isEmpty(), "No Minecraft version configured")
mainArtifact.validate()
additionalArtifacts.each { artifact -> artifact.validate() }
check(apiKey != null, "apiKey not set for project $id")
check(mainArtifact != null, "mainArtifact not set for project $id")
check(!gameVersionStrings.isEmpty(), "No Minecraft version configured for project $id")
mainArtifact.validate(id)
additionalArtifacts.each { artifact -> artifact.validate(id) }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.matthewprenger.cursegradle

import com.google.common.base.Strings

import static com.matthewprenger.cursegradle.Util.check

class CurseRelation implements Serializable {
Expand All @@ -8,20 +10,21 @@ class CurseRelation implements Serializable {
static {
CurseGradlePlugin.VALID_RELATIONS.each { relation ->
CurseRelation.metaClass."$relation" = { Object[] args ->
check(args.length == 1, "Invalid relation syntax for relation $relation")
projects.add(new Project(type: relation, slug: args[0]))
}
}
}

// Catches a missing method exception and gives a more user friendly error message
def methodMissing(String name, def args) {
throw new IllegalArgumentException("$name is not a valid relation type. Valid types are: $CurseGradlePlugin.VALID_RELATIONS")
check(false, "$name is not a valid relation type. Valid types are: $CurseGradlePlugin.VALID_RELATIONS")
}

Set<Project> projects = new HashSet<>()

void validate() {
projects.each { project -> project.validate() }
void validate(final def id) {
projects.each { project -> project.validate(id) }
}

static class Project implements Serializable {
Expand All @@ -36,9 +39,9 @@ class CurseRelation implements Serializable {
*/
String type

void validate() {
check(slug != null, "Project relation slug not set")
check(type != null && CurseGradlePlugin.VALID_RELATIONS.contains(type), "$type is not a valid relation type. Valid types are: $CurseGradlePlugin.VALID_RELATIONS")
void validate(final def id) {
check(!Strings.isNullOrEmpty(slug), "Project relation slug not set for relation in project $id")
check(CurseGradlePlugin.VALID_RELATIONS.contains(type), "Invalid relation type ($type) for relation in project $id. Valid options are: $CurseGradlePlugin.VALID_RELATIONS")
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.matthewprenger.cursegradle

import com.google.common.base.Strings
import com.matthewprenger.cursegradle.jsonresponse.CurseError
import com.matthewprenger.cursegradle.jsonresponse.UploadResponse
import org.apache.http.HttpResponse
Expand Down Expand Up @@ -33,9 +34,7 @@ class CurseUploadTask extends DefaultTask {
@TaskAction
run() {

if (''.equals(apiKey)) {
throw new RuntimeException("Project $projectId apiKey was not set")
}
Util.check(!Strings.isNullOrEmpty(apiKey), "CurseForge Project $projectId does not have an apiKey configured")

mainArtifact.resolve(project)

Expand Down Expand Up @@ -69,9 +68,9 @@ class CurseUploadTask extends DefaultTask {

post.addHeader('X-Api-Token', apiKey)
post.setEntity(MultipartEntityBuilder.create()
.addTextBody('metadata', json)
.addBinaryBody('file', file)
.build())
.addTextBody('metadata', json)
.addBinaryBody('file', file)
.build())

HttpResponse response = client.execute(post)

Expand All @@ -85,9 +84,9 @@ class CurseUploadTask extends DefaultTask {
InputStreamReader reader = new InputStreamReader(response.entity.content)
CurseError error = Util.gson.fromJson(reader, CurseError)
reader.close()
throw new RuntimeException("CurseForge Error: $error.errorCode: $error.errorMessage")
throw new RuntimeException("[CurseForge ${projectId}] Error Code ${error.errorCode}: ${error.errorMessage}")
} else {
throw new RuntimeException("Error: $response.statusLine.statusCode: $response.statusLine.reasonPhrase")
throw new RuntimeException("[CurseForge ${projectId}] HTTP Error Code $response.statusLine.statusCode: $response.statusLine.reasonPhrase")
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/groovy/com/matthewprenger/cursegradle/Util.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ class Util {
InputStreamReader reader = new InputStreamReader(response.entity.content)
CurseError error = gson.fromJson(reader, CurseError)
reader.close()
throw new RuntimeException("CurseForge Error: $error.errorCode: $error.errorMessage")
throw new RuntimeException("[CurseForge] Error Code ${error.errorCode}: ${error.errorMessage}")
} else {
throw new RuntimeException("Error: $response.statusLine.statusCode: $response.statusLine.reasonPhrase")
throw new RuntimeException("[CurseForge] HTTP Error Code $response.statusLine.statusCode: $response.statusLine.reasonPhrase")
}
}
}
Expand Down

0 comments on commit 5d7affe

Please sign in to comment.